C和vbnet的转换.docx

上传人:b****7 文档编号:23579190 上传时间:2023-05-18 格式:DOCX 页数:26 大小:27.25KB
下载 相关 举报
C和vbnet的转换.docx_第1页
第1页 / 共26页
C和vbnet的转换.docx_第2页
第2页 / 共26页
C和vbnet的转换.docx_第3页
第3页 / 共26页
C和vbnet的转换.docx_第4页
第4页 / 共26页
C和vbnet的转换.docx_第5页
第5页 / 共26页
点击查看更多>>
下载资源
资源描述

C和vbnet的转换.docx

《C和vbnet的转换.docx》由会员分享,可在线阅读,更多相关《C和vbnet的转换.docx(26页珍藏版)》请在冰豆网上搜索。

C和vbnet的转换.docx

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 

'''

XMLcomments

//Singleline 

/*Multiple 

   line */ 

///

XMLcommentsonsingleline 

/**

XMLcommentsonmultiplelines*/

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1