转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
VB.NET and C# 语法比较手册         ★★★★

VB.NET and C# 语法比较手册

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3638 更新时间:2009/4/23 19:01:40

VB.NET and C# Comparison
This is a quick reference guide to highlight some key syntactical differences between VB.NETand C#. Hope you find this useful!
Thank you to Tom Shelton, Fergus Cooney, and others for your input.



Comments
Data Types
Constants
Enumerations
Operators Choices
Loops
Arrays
Functions
Exception Handling Namespaces
Classes / Interfaces
Constructors / Destructors
Objects
Structs Properties
Delegates / Events
Console I/O
File I/O


VB.NET

C#

Comments '''' Single line only
Rem Single line only

// Single line
/* Multiple
    line  */
/// XML comments on single line
/** XML comments on multiple lines */

Data Types

Value Types
Boolean
Byte
Char   (example: "A"c)
Short, Integer, Long
Single, Double
Decimal
Date

Reference Types
Object
String

Dim x As Integer
Console.WriteLine(x.GetType())     '''' Prints System.Int32
Console.WriteLine(TypeName(x))  '''' Prints Integer

'''' Type conversion
Dim numDecimal As Single = 3.5
Dim numInt As Integer
numInt = CType(numDecimal, Integer)   '''' set to 4 (Banker''''s rounding)
numInt = CInt(numDecimal)  '''' same result as CType
numInt = Int(numDecimal)    '''' set to 3 (Int function truncates the decimal)

Value Types
bool
byte, sbyte
char   (example: ''''A'''')
short, ushort, int, uint, long, ulong
float, double
decimal
DateTime   (not a built-in C# type)

Reference Types
object
string

int x;
Console.WriteLine(x.GetType());    // Prints System.Int32
Console.WriteLine(typeof(int));      // Prints System.Int32


// Type conversion
double numDecimal = 3.5;
int numInt = (int) numDecimal;   // set to 3  (truncates decimal)

Constants Const MAX_STUDENTS As Integer = 25 const int MAX_STUDENTS = 25; Enumerations 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)     '''' Prints 1

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

Dim s As Type = GetType(Status)
Console.WriteLine([Enum].GetName(s, Status.Pass))    '''' 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(Status.Pass);    // Prints Pass Operators

Comparison
=  <  >  <=  >=  <>

Arithmetic
+  -  *  /
Mod
(integer division)
(raise to a power)

Assignment
=  +=  -=  *=  /=  \=  ^=  <<=  >>=  &=

Bitwise
And  AndAlso  Or  OrElse  Not  <<  >>

Logical
And  AndAlso  Or  OrElse  Not

Note: AndAlso and OrElse are for short-circuiting 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
+

Choices

greeting = IIf(age < 20, "What''''s up?", "Hello")

'''' One line doesn''''t require "End If", no "Else"
If language = "VB.NET" Then langType = "verbose"

'''' Use : to put two commands on same line
If x <> 100 Then x *= 5 : y *= 2  

'''' or to break up any long single command use _
If whenYouHaveAReally < longLine And itNeedsToBeBrokenInto2 > Lines Then _
  UseTheUnderscore(charToBreakItUp)

''''If x > 5 Then
  x *= y
ElseIf x = 5 Then
  x += y
ElseIf x < 10 Then
  x -= y
Else
  x /= y
End If

Select Case color   '''' Must be a primitive data type
  Case "pink", "red"
    r += 1
  Case "blue"
    b += 1
  Case "green"
    g += 1
  Case Else
    other += 1
End Select

greeting = age < 20 ? "What''''s up?" : "Hello";

if (x != 100) {    // Multiple statements must be enclosed in {}
  x *= 5;
  y *= 2;
}

No need for _ or : since ; is used to terminate each statement.




if
(x > 5)
  x *= y;
else if (x == 5)
  x += y;
else if (x < 10)
  x -= y;
else
  x /= y;



switch (color) {                          // Must be integer or string
  case "pink":
  case "red":    r++;    break;        // break is mandatory; no fall-through
  case "blue":   b++;   break;
  case "green": g++;   break;
  default:    other++;   break;       // break necessary on default
}

Loops Pre-test Loops: While c < 10
  c += 1
End While

Do Until c = 10 
  c += 1
Loop

Do While c < 10
  c += 1
Loop

For c = 2 To 10 Step 2
  Console.WriteLine(c)
Next


Post-test Loops: Do 
  c += 1
Loop While c < 10 Do

[1] [2] [3] [4]  下一页


[C语言系列]C# 过滤html,js,css代码 正则表达式  [C语言系列]C# DataGridView显示行号的两种方法
[C语言系列]C# WinForm 中Label自动换行 解决方法  [C语言系列]C# 线程调用主线程中的控件
[电脑应用]c# winform 打包部署 自定义界面 或设置开机启动  [C语言系列]C# 和 Linux 时间戳转换
[C语言系列]C#实现 WebBrowser中新窗口打开链接用默认或者指定…  [C语言系列]C#全角和半角转换
[C语言系列]c#WebBrowser查找并选择文本  [C语言系列]C#中实现WebBrowser控件的HTML源代码读写
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台