热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

VB与C#语法区别类比

1.变量声名C#:intx;Strings;Strings1,s2;Objectobj;ObjectobjnewObject();publicStringname;V
1.变量声名
C# :
int x;
String s;
String s1, s2;
Object obj;
Object obj = new Object();
public String name;
VB语法
Dim x As Integer
Dim s As String
Dim s1, s2 As String
Dim obj 'Implicitly Object
Dim obj As New Object()
Public name As String

 


2语句
C#:
Response.Write("123456");
VB:
Response.Write("123456")

3.注释语句
//这是c#注释
/*
C#的多行注

*/

VB:
'这是VB的注释

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[10];
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(10) 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 = 10;
double[] a = { 3.14, 4.53, 5.10 };
VB:
Dim s As String = "Hello World"
Dim i As Integer = 10
Dim a() As Double = { 3.14, 4.53, 5.10 }

8.判断语句(If 语句)
if (i!=10) {
...
}

VB:
If Not (i=10)
...
End If

9.分支语句(case 语句)
C#:
switch (a) {
case 0 :
...
break;
case 1 :
...
break;
case 2 :
...
break;
}
VB:
Select (a)
case 0 :
...
case1 :
...
case 2 :
...
End Select

10 .For循环语句
C#
for (int i&#61;0; i<10; i&#43;&#43;)
a(i) &#61; i;
VB:
Dim I As Integer
For I &#61; 0 To 10
a(i) &#61; i
Next

11. While 循环
C#:
int i &#61; 0;
while (i<10) {
Console.WriteLine(i.ToString());
i &#43;&#43;;
}
VB:
Dim I As Integer
I &#61; 0
Do While I <10
Console.WriteLine(I.ToString())
I &#61; &#43;&#43;
Loop
12 .字符串连接
C#:
String s1;
String s2 &#61; "hello";
s2 &#43;&#61; " world";
s1 &#61; s2 &#43; " !!!";
VB:
Dim s1, s2 As String
s2 &#61; "hello"
s2 &&#61; " world"
s1 &#61; 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 &#61; (MyObject)Session["XiaoYear"];
IMyObject iObj &#61; obj
VB:
Dim bj As MyObject
Dim iObj As IMyObject
obj &#61; Session("XiaoYear")
iObj &#61; CType(obj, IMyObject)


14. 数据类型转换
C#
int i &#61; 3;
String s &#61; i.ToString();
double d &#61; Double.Parse(s);
VB:
Dim i As Integer
Dim s As String
Dim d As Double

i &#61; 3
s &#61; i.ToString()
d &#61; CDbl(s)


15 .类的声明和继承
C#:
using System;
namespace MySpace {
public class A : B {
int x;
public F() { x &#61; 10; }
public void Add(int x) { this.x &#43;&#61; x; }
public int GetNum() { return x; }
}
}

VB:
Imports System
Namespace MySpace
Public Class A : Inherits B
Dim x As Integer
Public Sub New()
MyBase.New()
x &#61; 10
End Sub

Public Sub Add(x As Integer)
Me.x &#61; Me.x &#43; 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 &#61; new ConsoleCS();
}

}

VB "
Imports System
Public Class ConsoleVB
Public Sub New()
MyBase.New()
Console.WriteLine("Object Created")
End Sub

Public Shared Sub Main()
Console.WriteLine("Hello World")
Dim cvb As ConsoleVB
cvb &#61; New ConsoleVB()
End Sub

End Class


推荐阅读
author-avatar
DCPe-苦乐年华
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有