VB.NET 和 C# 都是微软公司推出的基于.NET 平台的编程语言,两者没有本质的区别,甚至有非常多的相似之处,下面的文章就向您展示了两者在编程习惯和语法上的区别:
文章出处:http://www.jb51.cc/article/p-oydkcroc-wv.html
VB.NET and C# Comparison
This is a quick reference guide to highlight some key syntactical differences between VB.NET (version 2) and C#. Hope you find this useful!
Thank you to Tom Shelton,Fergus Cooney,Steven Swafford,Gjuro Kladaric,and others for your contributions.
Also seeJava and C# Comparison.
|
|||||||||||
Imports System Namespace Hello Class HelloWorld Overloads Shared SubMain(ByVal args() As String) Dim name As String = "VB.NET" @H_502_87@'See if an argument was passedfrom the command line If args.Length = 1 Then name = args(0) Console.WriteLine("Hello," & name & "!") End Sub End Class End Namespace |
using System; namespace Hello { public class HelloWorld { public static voidMain(string[] args) { string name = "C#"; @H_502_87@// See if an argument was passed from the command line if (args.Length == 1) name = args[0]; Console.WriteLine("Hello," + name + "!"); } } } |
||||||||||
|
|||||||||||
@H_502_87@' Single line only REMSingle line only ''' <summary>XML comments</summary> |
@H_502_87@// Single line |
||||||||||
|
|||||||||||
@H_502_87@Value Types @H_502_87@Reference Types @H_502_87@Initializing @H_502_87@Type Information @H_502_87@Type Conversion |
@H_502_87@Value Types @H_502_87@Reference Types @H_502_87@Initializing @H_502_87@Type Information @H_502_87@Type Conversion |
||||||||||
|
|||||||||||
ConstMAX_STUDENTSAsInteger = 25 @H_502_87@' Can set to a const orvar; may be initialized in a constructor |
constint MAX_STUDENTS = 25; @H_502_87@// Can set to a const or var; may be initialized in a constructor |
||||||||||
|
|||||||||||
EnumAction Start [Stop]@H_502_87@' Stopis a reserved word Rewind Forward End Enum EnumStatus Flunk = 50 Pass = 70 Excel = 90 End Enum Dim a As Action = Action.Stop If a <> Action.Start Then _ Console.WriteLine(a.ToString & " is " & a) @H_502_87@' Prints "Stop is 1" Console.WriteLine(Status.Pass) @H_502_87@' Prints 70 Console.WriteLine(Status.Pass.ToString()) @H_502_87@' Prints Pass |
enumAction {Start,Stop,Rewind,Forward}; enumStatus {Flunk = 50,Pass = 70,Excel = 90}; Action a = Action.Stop; if (a != Action.Start) Console.WriteLine(a + " is " + (int) a);@H_502_87@// Prints "Stop is 1" Console.WriteLine((int) Status.Pass);@H_502_87@// Prints 70 Console.WriteLine(Status.Pass); @H_502_87@// Prints Pass |
||||||||||
|
|||||||||||
@H_502_87@Comparison @H_502_87@Arithmetic @H_502_87@Assignment @H_502_87@Bitwise @H_502_87@Logical Note:AndAlso and OrElse perform short-circuit logical evaluations @H_502_87@String Concatenation |
@H_502_87@Comparison @H_502_87@Arithmetic @H_502_87@Assignment @H_502_87@Bitwise @H_502_87@Logical Note:&& and||perform short-circuit logical evaluations @H_502_87@String Concatenation |
||||||||||
|
|||||||||||
greeting =IIf(age < 20,"What's up?","Hello") @H_502_87@' One line doesn't require "End If" @H_502_87@' Use : to put two commands on same line @H_502_87@' Preferred @H_502_87@' To break up any long single line use _ @H_502_87@'Ifx > 5Then Select Casecolor@H_502_87@' Must be a primitive data type |
greeting = age < 20?"What's up?":"Hello"; if(age < 20) @H_502_87@// Multiple statements must be enclosed in {} @H_502_87@No need for _ or : since ; is used to terminate each statement.
|
||||||||||
|
|||||||||||
@H_502_87@' Array or collection looping @H_502_87@' Breaking out of loops @H_502_87@' Continue to next iteration |
@H_502_87@Pre-test Loops: @H_502_87@// no "until" keyword
@H_502_87@// Breaking out of loops @H_502_87@// Continue to next iteration |
||||||||||
|
|||||||||||
|
|||||||||||
@H_502_87@' Pass by value (in,default),reference (in/out),andreference (out) Dim a = 1,b = 1,c As Integer @H_502_87@' cset to zero by default @H_502_87@' Accept variable number of arguments @H_502_87@' Optional parameters must belisted lastand must have a default value |
@H_502_87@// Pass by value (in,andreference (out) void TestFunc(int x,refint y,outint z) { x++; y++; z = 5; } int a = 1,c;@H_502_87@// c doesn't need initializing @H_502_87@// Accept variable number of arguments int total = Sum(4,1);@H_502_87@// returns 10 @H_502_87@/* C# doesn'tsupport optional arguments/parameters. Just create two different versions of the same function. */ |
||||||||||
|
|||||||||||
@H_502_87@Special character constants @H_502_87@' String concatenation (use & or +) @H_502_87@' Chars @H_502_87@' No string literal operator @H_502_87@' String comparison Console.WriteLine(mascot.Substring(2,3))@H_502_87@' Prints "son" @H_502_87@' String matching @H_502_87@' My birthday: Oct 12,1973 @H_502_87@' Mutable string |
@H_502_87@Escape sequences @H_502_87@// String concatenation @H_502_87@// Chars @H_502_87@// String literal @H_502_87@// String comparison Console.WriteLine(mascot.Substring(2,3));@H_502_87@// Prints "son" @H_502_87@// String matching @H_502_87@// My birthday: Oct 12,1973 @H_502_87@// Mutable string |
||||||||||
|
|||||||||||
@H_502_87@' Throw an exception @H_502_87@' Catch an exception @H_502_87@' Deprecated unstructured error handling |
@H_502_87@// Throw an exception @H_502_87@// Catch an exception |
||||||||||
|
|||||||||||
NamespaceHarding.Compsci.Graphics @H_502_87@' or NamespaceHarding ImportsHarding.Compsci.Graphics |
namespaceHarding.Compsci.Graphics { @H_502_87@// or namespaceHarding { usingHarding.Compsci.Graphics; |
||||||||||
|
|||||||||||
@H_502_87@Accessibility keywords @H_502_87@' Inheritance @H_502_87@' Interface definition @H_502_87@// Extending an interface @H_502_87@// Interface implementation |
@H_502_87@Accessibility keywords @H_502_87@// Inheritance
@H_502_87@// Extending an interface
|
||||||||||
|
|||||||||||
ClassSuperHero Private_powerLevel As Integer Public SubNew() _powerLevel = 0 End Sub Public SubNew(ByVal powerLevel As Integer) Me._powerLevel = powerLevel End Sub Protected Overrides SubFinalize() @H_502_87@' Desctructor code to free unmanaged resources MyBase.Finalize() End Sub End Class |
classSuperHero { |
||||||||||
|
|||||||||||
Dim hero As SuperHero = New SuperHero Withhero hero.Defend("Laura Jones") Dim hero2 As SuperHero = hero@H_502_87@' Both reference the same object hero =Nothing@H_502_87@' Free the object If heroIsNothingThen _ Dim obj As Object = New SuperHero @H_502_87@' Mark object for quick disposal |
SuperHero hero = new SuperHero(); @H_502_87@// No "With" construct hero.Defend("Laura Jones");
hero =null;@H_502_87@// Free the object if (hero ==null) Object obj = new SuperHero(); using(StreamReader reader = File.OpenText("test.txt")) { string line; while ((line = reader.ReadLine()) != null) Console.WriteLine(line); } |
||||||||||
|
|||||||||||
StructureStudentRecord Dim stu As StudentRecord = New StudentRecord("Bob",3.5) |
structStudentRecord { public string name; public float gpa; public StudentRecord(string name,float gpa) { this.name = name; this.gpa = gpa; } } StudentRecord stu = new StudentRecord("Bob",3.5f); |
||||||||||
|
|||||||||||
Private _size As Integer foo.Size += 1 |
private int _size; public int Size { get{ return _size; } set{ if (value < 0) _size = 0; else _size = value; } }
|
||||||||||
|
|||||||||||
DelegateSub MsgArrivedEventHandler(ByVal message As String) EventMsgArrivedEvent As MsgArrivedEventHandler @H_502_87@' or to define an event which declares a delegate implicitly AddHandlerMsgArrivedEvent,AddressOfMy_MsgArrivedCallback Imports System.Windows.Forms DimWithEventsMyButton As Button@H_502_87@' WithEvents can't be used on local variable Private Sub MyButton_Click(ByVal sender As System.Object,_ |
delegatevoid MsgArrivedEventHandler(string message); eventMsgArrivedEventHandler MsgArrivedEvent; @H_502_87@// Delegates must be used with events in C#
Button MyButton = new Button(); private void MyButton_Click(object sender,System.EventArgs e) { |
||||||||||
|
|||||||||||
Console.Write("What's your name? ") |
Console.Write("What's your name? ");
|
||||||||||
|
|||||||||||
Imports System.IO @H_502_87@' Write out to text file @H_502_87@' Read all lines from text file @H_502_87@' Write out to binary file @H_502_87@' Read from binary file |
using System.IO; @H_502_87@// Write out to text file @H_502_87@// Read all lines from text file @H_502_87@// Write out to binary file @H_502_87@// Read from binary file |