VB和c#语法对照Word文件下载.docx
- 文档编号:18414927
- 上传时间:2022-12-16
- 格式:DOCX
- 页数:17
- 大小:24.56KB
VB和c#语法对照Word文件下载.docx
《VB和c#语法对照Word文件下载.docx》由会员分享,可在线阅读,更多相关《VB和c#语法对照Word文件下载.docx(17页珍藏版)》请在冰豆网上搜索。
/*Multiple
line
*/
///<
XMLcommentsonsingleline<
/**<
XMLcommentsonmultiplelines<
DataTypes
ValueTypes
Boolean
Byte,SByte
Char
Short,UShort,Integer,UInteger,Long,ULong
Single,Double
Decimal
Date
ReferenceTypes
Object
String
Initializing
DimcorrectAsBoolean=True
DimbAsByte=&
H2A
hex
DimoAsByte=&
O52
octal
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
TypeInformation
DimxAsInteger
Console.WriteLine(x.GetType())
PrintsSystem.Int32
Console.WriteLine(GetType(Integer))
PrintsSystem.Int32
Console.WriteLine(TypeName(x))
PrintsInteger
TypeConversion
DimdAsSingle=3.5
DimiAsInteger=CType(d,Integer)
setto4(Banker'
srounding)
i=CInt(d)
sameresultasCType
i=Int(d)
setto3(Intfunctiontruncatesthedecimal)
bool
byte,sbyte
char
short,ushort,int,uint,long,ulong
float,
double
decimal
DateTime
(notabuilt-inC#
type)
object
string
boolcorrect=true;
byteb=0x2A;
//hex
objectperson=null;
stringname="
chargrade='
B'
DateTimetoday=DateTime.Parse("
12/31/200712:
00"
decimalamount=35.99m;
floatgpa=2.9f;
doublepi=3.14159265;
longlTotal=123456L;
shortsTotal=123;
ushortusTotal=123;
uintuiTotal=123;
ulongulTotal=123;
intx;
Console.WriteLine(x.GetType());
//PrintsSystem.Int32
Console.WriteLine(typeof(int));
//PrintsSystem.Int32
Console.WriteLine(x.GetType().Name);
//printsInt32
TypeConversion
floatd=3.5f;
inti=(int)d;
//setto
3
(truncatesdecimal)
Constants
Const
MAX_STUDENTSAsInteger=25
Cansettoaconstor
var;
maybeinitializedinaconstructor
ReadOnlyMIN_DIAMETERAsSingle=4.93
constintMAX_STUDENTS=25;
//Cansettoaconstorvar;
maybeinitializedinaconstructor
readonlyfloatMIN_DIAMETER=4.93f;
Enumerations
EnumAction
Start
[Stop]
Stop
isareservedword
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
enumAction{Start,Stop,Rewind,Forward};
enumStatus{Flunk=50,Pass=70,Excel=90};
Actiona=Action.Stop;
if(a!
=Action.Start)
Console.WriteLine(a+"
+(int)a);
//Prints"
Console.WriteLine((int)Status.Pass);
//Prints70
Console.WriteLine(Status.Pass);
//PrintsPass
Operators
Comparison
=
>
Arithmetic
+
-
*
/
Mod
\
(integerdivision)
^
(raisetoapower)
Assignment
+=
-=
*=
/=
\=
^=
<
=
=
Bitwise
And
Or
Xor
Not
Logical
AndAlso
OrElse
And
Not
Note:
AndAlsoandOrElseperformshort-circuitlogicalevaluations
StringConcatenation
&
==
!
%
(mod)
/
(integerdivisionifbothoperandsareints)
Math.Pow(x,y)
-=
*=
/=
%=
|=
^=
++
--
|
^
~
||
and
||
performshort-circuitlogicalevaluations
+
Choices
greeting=IIf(age<
20,"
What'
sup?
"
Hello"
)
Onelinedoesn'
trequire"
EndIf"
Ifage<
20Thengreeting="
Elsegreeting="
Use:
toputtwocommandsonsameline
Ifx<
100Andy<
5Thenx*=5:
y*=2
Preferred
5Then
x*=5
y*=2
EndIf
Tobreakupanylongsinglelineuse_
IfwhenYouHaveAReally<
longLineAnd_
itNeedsToBeBrokenInto2
Lines
Then_
UseTheUnderscore(charToBreakItUp)
Ifx>
5Then
x*=y
ElseIf
x=5Then
x+=y
ElseIfx<
10Then
x-=y
Else
x/=y
SelectCasecolor
Mustbeaprimitivedatatype
Case"
pink"
red"
r+=1
blue"
b+=1
green"
g+=1
CaseElse
other+=1
EndSelect
greeting=age<
20?
:
if(age<
20)
greeting="
else
//Multiplestatementsmustbeenclosedin{}
if(x!
=100&
y<
5){
x*=5;
y*=2;
}
Noneedfor_or:
since;
isusedtoterminateeachstatement.
if(x>
5)
x*=y;
elseif(x==5)
x+=y;
elseif(x<
10)
x-=y;
else
x/=y;
//Everycasemustendwithbreakorgotocase
switch(color)
{
//Mustbeintegerorstring
case"
:
r++;
break;
b++;
g++;
default:
other++;
//breaknecessaryondefault
Loops
Pre-testLoops:
Whilec<
10
c+=1
EndWhile
DoUntilc=10
c
+=1
Loop
DoWhilec<
Loop
Forc=2To10Step2
Console.WriteLine(c)
Next
Post-testLoops:
Do
LoopWhilec<
LoopUntilc=10
Arrayorcollectionlooping
DimnamesAsString()={"
Fred"
Sue"
Barney"
ForEachsAsString
Innames
Console.WriteLine(s)
Breakingoutofloops
DimiAsInteger=0
While(True)
If(i=5)ThenExitWhile
i+=1
Continuetonextiteration
Fori=0To4
Ifi<
4ThenContinueFor
Console.WriteLine(i)
Onlyprints4
//no"
until"
keyword
while(c<
c++;
for(c=2;
c<
=10;
c+=2)
Console.WriteLine(c);
Post-testLoop:
do
10);
//Arrayorcollectionlooping
string[]names={"
};
foreach(stringsinnames)
Console.WriteLine(s);
//Breakingoutofloops
inti=0;
while(true){
if(i==5)
i++;
//Continuetonextiteration
for(i=0;
i<
5;
i++){
if(i<
4)
continue;
Console.WriteLine(i);
//Onlyprints4
Arrays
Dimnums()AsInteger={1,2,3}
ForiAsInteger=0Tonums.Length-1
Console.WriteLine(nums(i))
4istheindexofthelastelement,soitholds5elements
Dimnames(4)AsString
names(0)="
David"
names(5)="
Bobby"
ThrowsSystem.IndexOutOfRangeException
Resizethearray,keepingtheexistingvalues(Preserveisoptional)
ReDimPreservenames(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;
nums.Length;
i++)
Console.WriteLine(nums[i]);
//5isthesizeofthearray
string[]names=newstring[5];
names[0]="
names[5]="
//ThrowsSystem.IndexOutOfRangeException
//C#can'
tdynamicallyresizeanarray.
Justcopyintonewarray.
string[]names2=newstring[7];
Array.Copy(names,names2,names.Length);
//ornames.CopyTo(names2,0);
float[,]twoD=newfloat[rows,cols];
twoD[2,0]=4.5f;
int[][]jagged=newint[3][]{
newint[5],newint[2],newint[3]};
jagged[0][4]=5;
Functions
Passbyvalue(in,default),reference(in/out),and
reference(out)
SubTestFunc(ByValxAsInteger,ByRefyAsInteger,ByRefzAsInteger)
x+=1
y+=1
z=5
EndSub
Dima=1,b=1,cAsInteger
settozerobydefault
TestFunc(a,b,c)
Console.WriteLine("
{0}{1}{2}"
a,b,c)
1
25
Acceptvariablenumberofarguments
FunctionSum(ByValParamArraynumsAsInteger())AsInteger
Sum=0
ForEachiAsIntegerInnums
Sum+=i
Next
EndFunction
OruseReturnstatementlikeC#
DimtotalAsInteger=Sum(4,3,2,1)
returns10
Optionalparametersmustbe
listedlast
andmusthaveadefaultvalue
SubSayHello(ByValnameAsString,OptionalByValprefixAsString="
Greetings,"
prefix&
name)
EndSub
SayHello("
Strangelove"
Dr."
Madonna"
//Passbyvalue(in,default),reference(in/out),and
reference(out)
voidTestFunc(intx,refinty,outintz){
x++;
y++;
z=5;
inta=1,b=1,c;
//cdoesn'
tneedinitializing
TestFunc(a,refb,outc);
a,b,c);
//125
//Acceptvariablenum
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VB c# 语法 对照
![提示](https://static.bdocx.com/images/bang_tan.gif)