vb语法和c语言语法,VB和c_语法对照

本文详细比较了VB.NET和C#在程序结构、注释、数据类型、常量、枚举、运算符、选择结构、循环、数组、函数、命名空间、类/接口、构造/析构函数、使用对象、结构、属性、委托/事件以及控制台和文件I/O等方面的语法差异,帮助开发者理解和转换两种语言的代码。
摘要由CSDN通过智能技术生成

VB.NET

Program Structure

C#

Imports System

Namespace Hello

Class HelloWorld

Overloads Shared Sub

Main(ByVal args() As String)

Dim name As String = "VB.NET"

'See if an argument was

passed from 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 void Main(string[]

args) {

string name = "C#";

// See if an argument was passed from the

command line

if (args.Length == 1)

name = args[0];

Console.WriteLine("Hello, " + name + "!");

}

}

}

VB.NET

Comments

C#

' Single line onlyREMSingle

line only

'''

XML

comments

// Single line///

XML comments on single

line

VB.NET

Data Types

C#

Value TypesBoolean

Byte, SByte

Char

Short, UShort, Integer, UInteger, Long, ULong

Single, Double

Decimal

Date

Reference TypesObject

String

InitializingDim correct As Boolean = True

Dim b As Byte = &H2A  'hex

Dim o As Byte = &O52  'octal

Dim person As Object = Nothing

Dim name As String = "Dwight"

Dim grade As Char = "B"c

Dim today As Date = #12/31/2007 12:15:00 PM#

Dim amount As Decimal = 35.99@

Dim gpa As Single = 2.9!

Dim pi As Double = 3.14159265

Dim lTotal As Long = 123456L

Dim sTotal As Short = 123S

Dim usTotal As UShort = 123US

Dim uiTotal As UInteger = 123UI

Dim ulTotal As ULong = 123UL

Type InformationDim x

As

Integer

Console.WriteLine(x.GetType()) ' Prints

System.Int32

Console.WriteLine(GetType(Integer))  ' Prints System.Int32

Console.WriteLine(TypeName(x)) ' Prints Integer

Type ConversionDim d As Single = 3.5

Dim i As Integer = CType(d,

Integer) ' set to 4 (Banker's rounding)

i = CInt(d) ' same result as CType

i = Int(d) ' set to 3 (Int function truncates the

decimal)

Value Typesbool

byte, sbyte

char

short, ushort, int, uint, long, ulong

float, double

decimal

DateTime (not a built-in

C# type)

Reference Typesobject

string

Initializingbool correct = true;

byte b = 0x2A;  //

hex

object person = null;

string name = "Dwight";

char grade = 'B';

DateTime today = DateTime.Parse("12/31/2007 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;

Type Informationint x;

Console.WriteLine(x.GetType()); // Prints

System.Int32

Console.WriteLine(typeof(int)); // Prints

System.Int32

Console.WriteLine(x.GetType().Name);  // prints Int32

Type Conversion float d = 3.5f;

int i = (int)d; // set

to 3 (truncates

decimal)

VB.NET

Constants

C#

ConstMAX_STUDENTS

As

Integer = 25

' Can set to a const or var; may

be initialized in a constructorReadOnly MIN_DIAMETER As Single = 4.93

const int MAX_STUDENTS = 25;

// Can set to a const or var; may be initialized

in a constructor readonly float MIN_DIAMETER = 4.93f;

VB.NET

Enumerations

C#

Enum Action

Start [Stop] '

Stop is a reserved word

Rewind

Forward

End

Enum

Enum Status

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) ' Prints "Stop is 1"

Console.WriteLine(Status.Pass) ' Prints 70

Console.WriteLine(Status.Pass.ToString()) ' Prints

Pass

enum Action {Start, Stop, Rewind, Forward};

enum Status

{Flunk = 50, Pass = 70, Excel = 90};

Action a = Action.Stop;

if (a != Action.Start)

Console.WriteLine(a + " is " + (int)

a); //

Prints "Stop is 1"

Console.WriteLine((int)

Status.Pass); // Prints 70

Console.WriteLine(Status.Pass); //

Prints Pass

VB.NET

Operators

C#

Comparison=  <= >= <>

Arithmetic+ - * /

Mod

\ (integer

division)

^ (raise to a

power)

Assignment= += -= *= /= \=

^= <<=

>>= &=

BitwiseAnd  Or  Xor  Not  <<

>>

LogicalAndAlso  OrElse  And

Or  Xor  Not

Note: AndAlso and OrElse perform short-circuit logical

evaluations

String Concatenation&

Comparison==  <= >= !=

Arithmetic+ - * /

% (mod)

/ (integer division if

both operands are ints)

Math.Pow(x, y)

Assignment= += -=

*= /=  %= &= |= ^= <<= >>= ++ --

Bitwise&  |  ^

~  <<  >>

Logical&&  ||

&  |

^  !

Note: &&

and || perform short-circuit

logical evaluations

String Concatenation&

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值