分享
 
 
 

vb.net和c#语法比较

王朝c#·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

由于一些人对vb.net和c#选择方面存在一些困惑,其实只是语法习惯问题,我把它们的语法列出来比较一下,大家有个感性认识。

1.变量声名

C# 语法

int x;

String s;

String s1, s2;

Object o;

Object obj = new Object();

public String name;

VB语法

Dim x As Integer

Dim s As String

Dim s1, s2 As String

Dim o 'Implicitly Object

Dim obj As New Object()

Public name As String

2语句

C#:

Response.Write("中文c#技术站");

VB:

Response.Write("中文c#技术站")

3.注释语句

//中文c#技术站

/*

欢迎访问

中文c#技术站

*/

VB:

'中文c#技术站

4.获得URL 传递的变量

C#:

String s = Request.QueryString["Name"];

String value = Request.Cookies["key"];

VB:

Dim s, value As String

s = Request.QueryString("Name")

value = Request.Cookies("Key").Value

5.声明属性

C#:

public String name {

get {

...

return ...;

}

set {

... = value;

}

}

VB:

Public Property Name As String

Get

...

Return ...;

End Get

Set

... = Value;

End Set

End Property

6.数组

C#

String[] a = new String[3];

a[0] = "1";

a[1] = "2";

a[2] = "3";

//二维数组

String[][] a = new String[3][3];

a[0][0] = "1";

a[1][0] = "2";

a[2][0] = "3";

VB:

Dim a(3) As String

a(0) = "1"

a(1) = "2"

a(2) = "3"

Dim a(3,3) As String

a(0,0) = "1"

a(1,0) = "2"

a(2,0) = "3"

Dim a() As String

a(0,0) = "1"

a(1,0) = "2"

a(2,0) = "3"

Dim a(,) As String

a(0,0) = "1"

a(1,0) = "2"

a(2,0) = "3"

7变量初始化

C#:

String s = "Hello World";

int i = 1

double[] a = { 3.00, 4.00, 5.00 };

VB:

Dim s As String = "Hello World"

Dim i As Integer = 1

Dim a() As Double = { 3.00, 4.00, 5.00 }

8;判断语句(If 语句)

if (Request.QueryString != null) {

...

}

VB:

If Not (Request.QueryString = Null)

...

End If

9.分支语句(case 语句)

C#:

switch (FirstName) {

case "John" :

...

break;

case "Paul" :

...

break;

case "Ringo" :

...

break;

}

VB:

Select (FirstName)

case "John" :

...

case "Paul" :...

case "Ringo" :

...

End Select

10 For循环语句

C#

for (int i=0; i<3; i++)

a(i) = "test";

VB:

Dim I As Integer

For I = 0 To 2

a(I) = "test"

Next

11 While 循环

C#:

int i = 0;

while (i<3) {

Console.WriteLine(i.ToString());

i += 1;

}

VB:

Dim I As Integer

I = 0

Do While I < 3

Console.WriteLine(I.ToString())

I = I + 1

Loop

12 字符串连接

C#:

String s1;

String s2 = "hello";

s2 += " world";

s1 = s2 + " !!!";

VB:

Dim s1, s2 As String

s2 = "hello"

s2 &= " world"

s1 = s2 & " !!!"

声明事件

C#:

void MyButton_Click(Object sender,

EventArgs E) {

...

}

VB:

Sub MyButton_Click(Sender As Object,

E As EventArgs)

...

End Sub

13 声明Object

C#

MyObject obj = (MyObject)Session["Some Value"];

IMyObject iObj = obj

VB:

Dim bj As MyObject

Dim iObj As IMyObject

obj = Session("Some Value")

iObj = CType(obj, IMyObject)

14 数据类型转换

C#

int i = 3;

String s = i.ToString();

double d = Double.Parse(s);

VB:

Dim i As Integer

Dim s As String

Dim d As Double

i = 3

s = i.ToString()

d = CDbl(s)

15 类的声明和继承

C#:

using System;

namespace MySpace {

public class Foo : Bar {

int x;

public Foo() { x = 4; }

public void Add(int x) { this.x += x; }

public int GetNum() { return x; }

}

}

VB:

Imports System

Namespace MySpace

Public Class Foo : Inherits Bar

Dim x As Integer

Public Sub New()

MyBase.New()

x = 4

End Sub

Public Sub Add(x As Integer)

Me.x = Me.x + x

End Sub

Public Function GetNum() As Integer

Return x

End Function

End Class

End Namespace

16 声明类的主函数

C#:

using System;

public class ConsoleCS {

public ConsoleCS() {

Console.WriteLine("Object Created");

}

public static void Main (String[] args) {

Console.WriteLine("Hello World");

ConsoleCS ccs = new ConsoleCS();

}

}

VB

Imports System

Public Class ConsoleVB

Public Sub New()

MyBase.New()

Console.WriteL

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有