C和vbnet的转换.docx
- 文档编号:23579190
- 上传时间:2023-05-18
- 格式:DOCX
- 页数:26
- 大小:27.25KB
C和vbnet的转换.docx
《C和vbnet的转换.docx》由会员分享,可在线阅读,更多相关《C和vbnet的转换.docx(26页珍藏版)》请在冰豆网上搜索。
C和vbnet的转换
今天在看30个学习必须收藏的的时候看到了这篇文章,感觉很好,以前都是自己看MSDN然后自己比较,现在又了这个感觉方便多了,很值得收藏!
这个是原文地址:
VB.NETandC#Comparison
正文开始:
ProgramStructure
Comments
DataTypes
Constants
Enumerations
Operators
Choices
Loops
Arrays
Functions
Strings
RegularExpressions
ExceptionHandling
Namespaces
Classes/Interfaces
Constructors/Destructors
UsingObjects
Structs
Properties Updated!
Delegates/Events
Generics New!
ConsoleI/O
FileI/O
VB.NET
ProgramStructure
C#
ImportsSystem
NamespaceHello
ClassHelloWorld
OverloadsSharedSub Main(ByValargs()AsString)
DimnameAsString="VB.NET"
'Seeifanargumentwaspassedfromthecommandline
Ifargs.Length=1Thenname=args(0)
Console.WriteLine("Hello,"&name&"!
")
EndSub
EndClass
EndNamespace
usingSystem;
namespaceHello{
publicclassHelloWorld{
publicstaticvoid Main(string[]args){
stringname="C#";
//Seeifanargumentwaspassedfromthecommandline
if(args.Length==1)
name=args[0];
Console.WriteLine("Hello,"+name+"!
");
}
}
}
VB.NET
Comments
C#
'Singlelineonly
REM Singlelineonly
'''
//Singleline
/*Multiple
line */
///
/**
VB.NET
DataTypes
C#
ValueTypes
Boolean
Byte,SByte
Char
Short,UShort,Integer,UInteger,Long,ULong
Single,Double
Decimal
Date
ReferenceTypes
Object
String
Initializing
DimcorrectAsBoolean=True
DimbAsByte=&H2A 'hexor&O52foroctal
DimpersonAsObject=Nothing
DimnameAsString="Dwight"
DimgradeAsChar="B"c
DimtodayAsDate=#12/31/200712:
15:
00PM#
DimamountAsDecimal=35.99
DimgpaAsSingle=2.9!
DimpiAsDouble=3.14159265
DimlTotalAsLong=123456L
DimsTotalAsShort=123S
DimusTotalAsUShort=123US
DimuiTotalAsUInteger=123UI
DimulTotalAsULong=123UL
ImplicitlyTypedLocalVariables
Dims="Hello!
"
Dimnums=NewInteger(){1,2,3}
Dimhero=NewSuperHeroWith{.Name="Batman"}
TypeInformation
Dim x As Integer
Console.WriteLine(x.GetType()) 'PrintsSystem.Int32
Console.WriteLine(GetType(Integer)) 'PrintsSystem.Int32
Console.WriteLine(TypeName(x)) 'PrintsInteger
DimcasNewCircle
If TypeOf c Is ShapeThen_
Console.WriteLine("cisaShape")
TypeConversion/Casting
DimdAsSingle=3.5
DimiAsInteger= CType(d,Integer) 'setto4(Banker'srounding)
i= CInt(d) 'sameresultasCType
i= Int(d) 'setto3(Intfunctiontruncatesthedecimal)
DimoAsObject=2
i= DirectCast(o,Integer) 'ThrowsInvalidCastExceptioniftypecastfails
DimsAsNewShape
DimcAsCircle= TryCast(s,Circle) 'ReturnsNothingiftypecastfails
ValueTypes
bool
byte,sbyte
char
short,ushort,int,uint,long,ulong
float,double
decimal
DateTime (notabuilt-inC#type)
ReferenceTypes
object
string
Initializing
boolcorrect=true;
byteb=0x2A; //hex
objectperson=null;
stringname="Dwight";
chargrade='B';
DateTimetoday=DateTime.Parse("12/31/200712:
15:
00");
decimalamount=35.99m;
floatgpa=2.9f;
doublepi=3.14159265;
longlTotal=123456L;
shortsTotal=123;
ushortusTotal=123;
uintuiTotal=123;
ulongulTotal=123;
ImplicitlyTypedLocalVariables
vars="Hello!
";
varnums=newint[]{1,2,3};
varhero=newSuperHero(){Name="Batman"};
TypeInformation
intx;
Console.WriteLine(x.GetType()); //PrintsSystem.Int32
Console.WriteLine(typeof(int)); //PrintsSystem.Int32
Console.WriteLine(x.GetType().Name); //printsInt32
Circlec=newCircle();
if(c is Shape)
Console.WriteLine("cisaShape");
TypeConversion/Casting
floatd=3.5f;
i= Convert.ToInt32(d); //Setto4(rounds)
inti= (int)d; //setto3(truncatesdecimal)
objecto=2;
inti=(int)o; //ThrowsInvalidCastExceptioniftypecastfails
Shapes=newShape();
Circlec=s as Circle; //Returnsnulliftypecastfails
VB.NET
Constants
C#
Const MAX_STUDENTS As Integer=25
'Cansettoaconstorvar;maybeinitializedinaconstructor
ReadOnly MIN_DIAMETER As Single=4.93
const intMAX_STUDENTS=25;
//Cansettoaconstorvar;maybeinitializedinaconstructor
readonly floatMIN_DIAMETER=4.93f;
VB.NET
Enumerations
C#
Enum Action
Start
[Stop] 'Stopisareservedword
Rewind
Forward
EndEnum
Enum Status
Flunk=50
Pass=70
Excel=90
EndEnum
DimaAsAction=Action.Stop
Ifa<>Action.StartThen_
Console.WriteLine(a.ToString&"is"&a) 'Prints"Stopis1"
Console.WriteLine(Status.Pass) 'Prints70
Console.WriteLine(Status.Pass.ToString()) 'PrintsPass
enum Action{Start,Stop,Rewind,Forward};
enum Status{Flunk=50,Pass=70,Excel=90};
Actiona=Action.Stop;
if(a!
=Action.Start)
Console.WriteLine(a+"is"+(int)a); //Prints"Stopis1"
Console.WriteLine((int)Status.Pass); //Prints70
Console.WriteLine(Status.Pass); //PrintsPass
VB.NET
Operators
C#
Comparison
= < > <= >= <>
Arithmetic
+ - * /
Mod
\ (integerdivision)
^ (raisetoapower)
Assignment
= += -= *= /= \= ^= <<= >>= &=
Bitwise
And Or Xor Not << >>
Logical
AndAlso OrElse And Or Xor Not
Note:
AndAlsoandOrElseperformshort-circuitlogicalevaluations
StringConcatenation
&
Comparison
== < > <= >= !
=
Arithmetic
+ - * /
% (mod)
/ (integerdivisionifbothoperandsareints)
Math.Pow(x,y)
Assignment
= += -= *= /= %= &= |= ^= <<= >>= ++ --
Bitwise
& | ^ ~ << >>
Logical
&& || & | ^ !
Note:
&&and||performshort-circuitlogicalevaluations
StringConcatenation
+
VB.NET
Choices
C#
'Ternary/Conditionaloperator(Iffevaluates2ndand3rdexpressions)
greeting= If(age<20,"What'sup?
","Hello")
'Onelinedoesn'trequire"EndIf"
If age<20 Then greeting="What'sup?
"
If age<20 Then greeting="What'sup?
" Else greeting="Hello"
'Use:
toputtwocommandsonsameline
If x<>100AndAlsoy<5 Then x*=5 :
y*=2
'Preferred
If x<>100AndAlsoy<5 Then
x*=5
y*=2
EndIf
'Tobreakupanylongsinglelineuse_
If whenYouHaveAReally itNeedsToBeBrokenInto2>Lines Then _ UseTheUnderscore(charToBreakItUp) 'If x>5 Then x*=y ElseIf x=5OrElseyMod2=0 Then x+=y ElseIf x<10 Then x-=y Else x/=y EndIf SelectCase color 'Mustbeaprimitivedatatype Case "pink","red" r+=1 Case "blue" b+=1 Case "green" g+=1 CaseElse other+=1 EndSelect //Ternary/Conditionaloperator greeting=age<20 ? "What'sup? " : "Hello"; if (age<20) greeting="What'sup? "; else greeting="Hello"; //Multiplestatementsmustbeenclosedin{} if (x! =100&&y<5){ x*=5; y*=2; } Noneedfor_or: since;isusedtoterminateeachstatement. if (x>5) x*=y; else if(x==5||y%2==0) x+=y; else if(x<10) x-=y; else x/=y; //Everycasemustendwithbreakorgotocase switch (color){ //Mustbeintegerorstring case "pink": case "red": r++; break; case "blue": b++; break; case "green": g++; break; default: other++; break; //breaknecessaryondefault } VB.NET Loops C# Pre-testLoops: While c<10 c+=1 EndWhile DoUntil c=10 c+=1 Loop DoWhile c<10 c+=1 Loop For c=2 To 10 Step2 Console.WriteLine(c) Next Post-testLoops: Do c+=1 LoopWhile c<10 Do c+=1 LoopUntil c=10 ' Arrayorcollectionlooping DimnamesAsString()={"Fred","Sue","Barney"} ForEach sAsString In names Console.WriteLine(s) Next 'Breakingoutofloops DimiAsInteger=0 While(True) If(i=5)Then ExitWhile i+=1 EndWhile 'Continuetonextiteration Fori=0To4 Ifi<4Then ContinueFor Console.WriteLine(i) 'Onlyprints4 Next Pre-testLoops: //no"until"keyword while (c<10) c++; for (c=2;c<=10;c+=2) Console.WriteLine(c); Post-testLoop: do c++; while (c<10); //Arrayorcollectionlooping string[]names={"Fred","Sue","Barney"}; foreach (strings in names) Console.WriteLine(s); //Breakingoutofloops inti=0; while(true){ if(i==5) break; i++; } //Continuetonextiteration for(i=0;i<5;i++){ if(i<4) continue; Console.WriteLine(i); //Onlyprints4 } VB.NET Arrays C# Dimnums() AsInteger={1,2,3} ForiAsInteger=0Tonums.Length-1 Console.WriteLine(nums(i)) Next '4istheindexofthelastelement,soitholds5elements Dimnames(4)AsString names(0)="David" names(5)="Bobby" 'ThrowsSystem.IndexOutOfRangeException 'Resizethearray,keepingtheexistingvalues(Preserveisoptional) ReDimPreserve names(6) DimtwoD(rows-1,cols-1)AsSingle twoD(2,0)=4.5 Dimjagged()() AsInteger={_ NewInteger(4){},NewInteger (1){},NewInteger (2){}} jagged(0)(4)=5 int[] nums={1,2,3}; for(inti=0;i Console.WriteLine(nums[i]); //5isthesizeofthearray string[]names=newstring[5]; names[0]="David"; names[5]="Bobby"; //ThrowsSyste
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- vbnet 转换