注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

陈方林

世道如今已惯,此行道路依然

 
 
 

日志

 
 

【转载】VB和C#语法区别  

2013-11-27 21:03:21|  分类: VB |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
本文转载自迪 迪《VB和C#语法区别》

1

      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

      C# 语法

      int x;

      String s;

      String s1, s2;

      Object o;

      Object obj = new Object();

      public String name;    

2

      VB:

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

      语句 C#:

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

3

      VB:

      '中文c#技术站

注释语句

      //中文c#技术站

      /*

      欢迎访问

      ,

      中文c#技术站

      */

4

      VB:

      Dim s, value As String

      s = Request.QueryString("Name")

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

     C#获得URL 传递的变量

 

      C#:

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

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

5

VB:

      Public Property Name As String

      Get

      ...

      Return ...;

      End Get

      Set

      ... = Value;

      End Set

      End Property

声明属性       C#:

      public String name {

      get {

      ...

      return ...;

      }

      set {

      ... = value;

      }

      }    

6

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"

数组       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";

 

     

7

VB:

      Dim s As String = "Hello World"

      Dim i As Integer = 1

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

变量初始化 C#:

      String s = "Hello World";

      int i = 1

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

8

VB:

      If Not (Request.QueryString = Null)

      ...

      End If

;判断语句(If 语句)

 

      if (Request.QueryString != null) {

      ...

      }    

9

VB:

      Select (FirstName)

      case "John" :

      ...

      case "Paul" :

      ...

      case "Ringo" :

      ...

      End Select

分支语句(case 语句)       C#:

      switch (FirstName) {

      case "John" :

      ...

      break;

      case "Paul" :

      ...

      break;

      case "Ringo" :

      ...

      break;

      }      

10

VB:

      Dim I As Integer

      For I = 0 To 2

      a(I) = "test"

      Next

For循环语句    C#

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

      a(i) = "test";      

11

VB:

      Dim I As Integer

      I = 0

      Do While I < 3

      Console.WriteLine(I.ToString())

      I = I + 1

      Loop

While 循环       C#:

      int i = 0;

      while (i<3) {

      Console.WriteLine(i.ToString());

      i += 1;

      }

12

VB:

      Dim s1, s2 As String

      s2 = "hello"

      s2 &= " world"

      s1 = s2 & " !!!"

字符串连接      C#:

      String s1;

      String s2 = "hello";

      s2 += " world";

      s1 = s2 + " !!!";       

13

VB:

      Sub MyButton_Click(Sender As Object,

      E As EventArgs)

      ...

      End Sub

声明事件      C#:

      void MyButton_Click(Object sender,

      EventArgs E) {

      ...

      }

14

VB:

      Dim bj As MyObject

      Dim iObj As IMyObject

      obj = Session("Some Value")

      iObj = CType(obj, IMyObject)

声明Object      C#

      MyObject obj =

(MyObject)Session["Some Value"];

      IMyObject iObj = obj 

15

VB:

      Dim i As Integer

      Dim s As String

      Dim d As Double

      i = 3

      s = i.ToString()

      d = CDbl(s)

数据类型转换       C#

      int i = 3;

      String s = i.ToString();

      double d = Double.Parse(s); 

16

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

类的声明和继承       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; }

      }

      }

 

      

17

VB

      Imports System

      Public Class ConsoleVB

      Public Sub New()

      M

      yBase.New()

      Console.WriteLine("Object Created")

      End Sub

      Public Shared Sub Main()

      Console.WriteLine("Hello World")

      Dim cvb As ConsoleVB

      cvb = New ConsoleVB()

      End Sub

      End Class

声明类的主函数       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();

      }

      }

18

VB:

      Imports System

      Public Module ConsoleVB

      Public Sub Main()

      Console.WriteLine("Hello World")

      End Sub

      End Module

标准模块       C#

      using System;

      public class Module {

      public static void Main (String[] args) {

      Console.WriteLine("Hello World");

      }

      } 

 

 

 

 

 

 

  评论这张
 
阅读(189)| 评论(25)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017