《C#和vbnet的转换》由会员分享,可在线阅读,更多相关《C#和vbnet的转换(22页珍藏版)》请在金锄头文库上搜索。
1、今天在看30个学习必须收藏的的时候看到了这篇文章,感觉很好,以前都是自己看MSDN然后自己比较,现在又了这个感觉方便多了,很值得收藏!这个是原文地址:VB.NET and C# Comparison正文开始:Program StructureCommentsData TypesConstantsEnumerationsOperatorsChoicesLoopsArraysFunctionsStringsRegular ExpressionsException HandlingNamespacesClasses / InterfacesConstructors / DestructorsUsin
2、g ObjectsStructsPropertiesUpdated!Delegates / EventsGenericsNew!Console I/OFile I/OVB.NETProgram StructureC#Imports SystemNamespace Hello Class HelloWorld Overloads Shared SubMain(ByVal args() As String) Dim name As String = VB.NETSee if an argument was passed from the command line If args.Length =
3、1 Then name = args(0) Console.WriteLine(Hello, & name & !) End Sub End ClassEnd Namespaceusing System;namespace Hello public class HelloWorld public static voidMain(string args) string name = C#;/ See if an argument was passed from the command line if (args.Length = 1) name = args0; Console.WriteLin
4、e(Hello, + name + !); VB.NETCommentsC# Single line onlyREMSingle line only XML comments/ Single line/* Multiple line */ XML comments on single line/* XML comments on multiple lines */VB.NETData TypesC#Value TypesBooleanByte, SByteCharShort, UShort, Integer, UInteger, Long, ULongSingle, DoubleDecimal
5、DateReference TypesObjectStringInitializingDim correct As Boolean = TrueDim b As Byte = &H2Ahex or &O52 for octalDim person As Object = NothingDim name As String = DwightDim grade As Char = BcDim today As Date = #12/31/2007 12:15:00 PM#Dim amount As Decimal = 35.99Dim gpa As Single = 2.9!Dim pi As D
6、ouble = 3.14159265Dim lTotal As Long = 123456LDim sTotal As Short = 123SDim usTotal As UShort = 123USDim uiTotal As UInteger = 123UIDim ulTotal As ULong = 123ULImplicitly Typed Local VariablesDim s = Hello!Dim nums = New Integer() 1, 2, 3Dim hero = New SuperHero With .Name = BatmanType InformationDi
7、mxAsIntegerConsole.WriteLine(x.GetType() Prints System.Int32Console.WriteLine(GetType(Integer) Prints System.Int32Console.WriteLine(TypeName(x) Prints IntegerDim c as New CircleIfTypeOfcIsShape Then _ Console.WriteLine(c is a Shape)Type Conversion / CastingDim d As Single = 3.5Dim i As Integer =CTyp
8、e(d, Integer) set to 4 (Bankers rounding)i =CInt(d) same result as CTypei =Int(d) set to 3 (Int function truncates the decimal)Dim o As Object = 2i =DirectCast(o, Integer) Throws InvalidCastException if type cast failsDim s As New ShapeDim c As Circle =TryCast(s, Circle) Returns Nothing if type cast
9、 failsValue Typesboolbyte, sbytecharshort, ushort, int, uint, long, ulongfloat, doubledecimalDateTime(not a built-in C# type)Reference TypesobjectstringInitializingbool correct = true;byte b = 0x2A;/ hexobject person = null;string name = Dwight;char grade = B;DateTime today = DateTime.Parse(12/31/20
10、07 12:15:00);decimal amount = 35.99m;float gpa = 2.9f;double pi = 3.14159265;long lTotal = 123456L;short sTotal = 123;ushort usTotal = 123;uint uiTotal = 123;ulong ulTotal = 123;Implicitly Typed Local Variablesvar s = Hello!;var nums = new int 1, 2, 3 ;var hero = new SuperHero() Name = Batman ;Type
11、Informationint x;Console.WriteLine(x.GetType();/ Prints System.Int32Console.WriteLine(typeof(int);/ Prints System.Int32Console.WriteLine(x.GetType().Name);/ prints Int32Circle c = new Circle();if (cisShape) Console.WriteLine(c is a Shape);Type Conversion / Castingfloat d = 3.5f;i =Convert.ToInt32(d)
12、;/ Set to 4 (rounds)int i =(int)d;/ set to 3 (truncates decimal)object o = 2;int i = (int)o;/ Throws InvalidCastException if type cast failsShape s = new Shape();Circle c = sasCircle;/ Returns null if type cast failsVB.NETConstantsC#ConstMAX_STUDENTSAsInteger = 25 Can set to a const or var; may be i
13、nitialized in a constructorReadOnlyMIN_DIAMETERAsSingle = 4.93constint MAX_STUDENTS = 25;/ Can set to a const or var; may be initialized in a constructorreadonlyfloat MIN_DIAMETER = 4.93f;VB.NETEnumerationsC#EnumAction Start Stop Stop is a reserved word Rewind ForwardEnd EnumEnumStatus Flunk = 50 Pass = 70 Excel = 90End EnumDim a As Action = Action.StopIf a Action.Start Then _ Console.WriteLine(a.ToString & is & a