C# 与 VB.NET 对比
C# 与 VB.NET 对比
2008-06-20 15:30 by Anders Cui, 1462 阅读, 3 评论, 收藏, 编辑
2.15Constructors / Destructors
1.0 Introduction
1.1 Purpose & Scope
This document is to compare the syntax between the VB.Net and C#.Net for various coding concepts.
2.0 Comparison
2.1 Program Structure
|
VB.NET |
C#.NET |
|
Imports System Namespace Hello 'See if an argument was passed from the command line Console.WriteLine("Hello, " & name & "!") |
using System; namespace Hello { // See if an argument was passed from the command line Console.WriteLine("Hello, " + name + "!"); |
2.2 Comments
|
VB.NET |
C#.NET |
|
' Single line only |
// Single line |
2.3 Data Types
|
VB.NET |
C#.NET |
|
Value Types Reference Types Initializing Type Information Type Conversion |
Value Types Reference Types Initializing object person = null; Type Information Type Conversion |
2.4 Constants
|
VB.NET |
C#.NET |
|
Const MAX_STUDENTS As Integer = 25 ' Can set to a const or var; may be initialized in a constructor |
const int MAX_STUDENTS = 25; // Can set to a const or var; may be initialized in a constructor |
2.5 Enumerations
|
VB.NET |
C#.NET |
|
Enum Action Enum Status Console.WriteLine(Status.Pass) ' Prints 70 |
enum Action {Start, Stop, Rewind, Forward}; Action a = Action.Stop; Console.WriteLine((int) Status.Pass); // Prints 70 |
2.6 Operators
|
VB.NET |
C#.NET |
|
Comparison Arithmetic Assignment Bitwise Logical Note: AndAlso and OrElse perform short-circuit logical evaluations String Concatenation |
Comparison Arithmetic Assignment Bitwise Logical Note: && and || perform short-circuit logical evaluations String Concatenation |
2.7 Choices
|
VB.NET |
C#.NET |
|
greeting = IIf(age < 20, "What's up?", "Hello") ' One line doesn't require "End If" ' Use : to put two commands on same line ' Preferred ' To break up any long single line use _ 'If x > 5 Then Select Case color ' Must be a primitive data type |
greeting = age < 20 ? "What's up?" : "Hello"; if (age < 20) // Multiple statements must be enclosed in {} No need for _ or : since ; is used to terminate each statement. if (x > 5) // Every case must end with break or goto case |
2.8 Loops
|
VB.NET |
C#.NET |
||||||
|
Pre-test Loops:
Post-test Loops:
' Array or collection looping ' Breaking out of loops ' Continue to next iteration |
Pre-test Loops: // no "until" keyword for (c = 2; c <= 10; c += 2) Post-test Loop: do // Array or collection looping // Breaking out of loops // Continue to next iteration |
2.9 Arrays
|
VB.NET |
C#.NET |
|
Dim nums() As Integer = {1, 2, 3} ' 4 is the index of the last element, so it holds 5 elements ' Resize the array, keeping the existing values (Preserve is optional) Dim twoD(rows-1, cols-1) As Single Dim jagged()() As Integer = { _ |
int[] nums = {1, 2, 3}; // 5 is the size of the array // C# can't dynamically resize an array. Just copy into new array. float[,] twoD = new float[rows, cols]; int[][] jagged = new int[3][] { |
2.10 Functions
|
VB.NET |
C#.NET |
|
' Pass by value (in, default), reference (in/out), and reference (out) Dim a = 1, b = 1, c As Integer ' c set to zero by default ' Accept variable number of arguments Dim total As Integer = Sum(4, 3, 2, 1) ' returns 10 ' Optional parameters must be listed last and must have a default value SayHello("Strangelove", "Dr.") |
// Pass by value (in, default), reference (in/out), and reference (out) int a = 1, b = 1, c; // c doesn't need initializing // Accept variable number of arguments int total = Sum(4, 3, 2, 1); // returns 10 /* C# doesn't support optional arguments/parameters. Just create two different versions of the same function. */ void SayHello(string name) { |
2.11 Strings
|
VB.NET |
C#.NET |
|
Special character constants ' String concatenation (use & or +) ' Chars ' No string literal operator ' String comparison Console.WriteLine(mascot.Substring(2, 3)) ' Prints "son" ' String matching Imports System.Text.RegularExpressions ' More powerful than Like ' My birthday: Oct 12, 1973 ' Mutable string |
Escape sequences // String concatenation // Chars // String literal // String comparison Console.WriteLine(mascot.Substring(2, 3)); // Prints "son" // String matching using System.Text.RegularExpressions; // My birthday: Oct 12, 1973 // Mutable string |
2.12 Exception Handling
|
VB.NET |
C#.NET |
|
' Throw an exception ' Catch an exception ' Deprecated unstructured error handling |
// Throw an exception // Catch an exception |
2.13 Namespaces
|
VB.NET |
C#.NET |
|
Namespace Harding.Compsci.Graphics ' or Namespace Harding Imports Harding.Compsci.Graphics |
namespace Harding.Compsci.Graphics { // or namespace Harding { using Harding.Compsci.Graphics; |
2.14 Classes / Interfaces
|
VB.NET |
C#.NET |
|
Accessibility keywords ' Inheritance ' Interface definition // Extending an interface // Interface implementation |
Accessibility keywords // Inheritance
// Extending an interface
|
2.15 Constructors / Destructors
|
VB.NET |
C#.NET |
|
Class SuperHero Public Sub New() Public Sub New(ByVal powerLevel As Integer) Protected Overrides Sub Finalize() |
class SuperHero { public SuperHero() { public SuperHero(int powerLevel) { ~SuperHero() { |
2.16 Using Objects
|
VB.NET |
C#.NET |
|
Dim hero As SuperHero = New SuperHero With hero hero.Defend("Laura Jones") Dim hero2 As SuperHero = hero ' Both reference the same object hero = Nothing ' Free the object If hero IsNothing Then _ Dim obj As Object = New SuperHero ' Mark object for quick disposal |
SuperHero hero = new SuperHero(); // No "With" construct hero.Defend("Laura Jones"); SuperHero hero2 = hero; // Both reference the same object hero = null ; // Free the object if (hero == null) Object obj = new SuperHero(); // Mark object for quick disposal |
2.17 Structs
|
VB.NET |
C#.NET |
|
Structure StudentRecord Public Sub New(ByVal name As String, ByVal gpa As Single) Dim stu As StudentRecord = New StudentRecord("Bob", 3.5) stu2.name = "Sue" |
struct StudentRecord { public StudentRecord(string name, float gpa) { StudentRecord stu = new StudentRecord("Bob", 3.5f); stu2.name = "Sue"; |
2.18 Properties
|
VB.NET |
C#.NET |
|
Private _size As Integer Public Property Size() As Integer foo.Size += 1 |
private int _size; public int Size { foo.Size++; |
2.19 Delegates / Events
|
VB.NET |
C#.NET |
|
Delegate Sub MsgArrivedEventHandler(ByVal message As String) Event MsgArrivedEvent As MsgArrivedEventHandler ' or to define an event which declares a delegate implicitly AddHandler MsgArrivedEvent, AddressOfMy_MsgArrivedCallback Imports System.Windows.Forms Dim WithEvents MyButton As Button ' WithEvents can't be used on local variable Private Sub MyButton_Click(ByVal sender As System.Object, _ |
delegate void MsgArrivedEventHandler(string message); event MsgArrivedEventHandler MsgArrivedEvent; // Delegates must be used with events in C# MsgArrivedEvent += new MsgArrivedEventHandler(My_MsgArrivedEventCallback); using System.Windows.Forms; Button MyButton = new Button(); private void MyButton_Click(object sender, System.EventArgs e) { |
2.20 Console I/O
|
VB.NET |
C#.NET |
|
Console.Write("What's your name? ") Dim c As Integer |
Console.Write("What's your name? "); int c = Console.Read(); // Read single char |
2.21 File I/O
|
VB.NET |
C#.NET |
|
Imports System.IO ' Write out to text file ' Read all lines from text file ' Write out to binary file ' Read from binary file |
using System.IO; // Write out to text file // Read all lines from text file // Write out to binary file // Read from binary file |
C# 与 VB.NET 对比的更多相关文章
- ViewBag & ViewData
ViewBag 和ViewData 是ASP.NET MVC 开发当中大家使用很多的传递数据的方法 VB可以称为VD的一块语法糖, VB是使用C# 4.0动态特征, 使得VD也具有动态特性. 下面就是 ...
- VB6.0 和VB.NET 函数对比
VB6.0和VB.Net的对照表 VB6.0 VB.NET AddItem Object名.AddItem Object名.Items.Add ListBox1.Items.Add ComboBox1 ...
- C#跨平台手机应用开发工具Xamarin尝试 与Eclipse简单对比
Xamarin 支持使用C#开发基于Android.IOS.WindowsPhone应用开发,最大特点C#+跨平台,详细说明问度娘. 安装 研究 想体验研究的点击查看页面 Xamarin For Vi ...
- [VB] if 判断语句 和 If、IIf函数的比较
Module Module1 Sub Main() Dim s1 As String = "我是真的" Dim s2 As String = "我不是真的" D ...
- vb小程序浅析
系统 : Windows xp 程序 : BJCM10B 程序下载地址 :http://pan.baidu.com/s/1dFyXe29 要求 : 编写注册机 使用工具 : OD 可在看雪论坛中查找关 ...
- 【应用笔记】【AN004】VB环境下基于RS-485的4-20mA电流采集
版本:第一版作者:周新稳 杨帅 日期:20160226 =========================== 本资料高清PDF 下载: http://pan.baidu.com/s/1c1uuhLQ ...
- VB6中的引用传递 与 VB.NET中的引用传递的区别
首先注意一点,在VB6中缺省参数传递的方式是:引用传递,而在VB.NET中缺省参数传递的方式是:值传递. 然后我们看下面VB6中的引用传递与VB.NET中的引用传递的对比. VB6中的引用传递 Pri ...
- 【VB超简单入门】二、知识准备
在开始编程之前,需要先熟悉一下各种操作和术语,以后学习编程才能得心应手. 首先最重要的操作当然就是-电脑的开机关机啦~(开个玩笑哈哈),必须掌握软件的安装和卸载,还有能编写批处理程序对平时的使用也是很 ...
- ORM框架-VB/C#.Net实体代码生成工具(EntitysCodeGenerate)【ECG】4.5
摘要:VB/C#.Net实体代码生成工具(EntitysCodeGenerate)[ECG]是一款专门为.Net数据库程序开发量身定做的(ORM框架)代码生成工具,所生成的程序代码基于OO.ADO.N ...
随机推荐
- Java琐碎知识点
jps命令是JDK1.5提供的一条显示当前用户的所有java进程pid的指令,类似Linux上的ps命令简化版,Windows和linux/unix平台都可以用比较常用的参数:-q:只显示pid,不显 ...
- Minimum Sum(思维)
Problem 1603 - Minimum Sum Time Limit: 2000MS Memory Limit: 65536KB Total Submit: 563 Accepted ...
- 委托-异步调用-泛型委托-匿名方法-Lambda表达式-事件【转】
1. 委托 From: http://www.cnblogs.com/daxnet/archive/2008/11/08/1687014.html 类是对象的抽象,而委托则可以看成是函数的抽象.一个委 ...
- android分割线
http://blog.csdn.net/dekunchenivan/article/details/6640804 在Android布局文件layout中设置水平分割线: <View ...
- 第三章:挖掘SimpleSection.o
1.查看.o目标文件用objdump 命令, 参数“-h"就是把ELF文件的各个段的基本信息打印出来.也可以使用-X打印更多的信息. 段的属性,Size是段的长度,FIle off 是段开始 ...
- win7运行sqlplus报错“SP2-1503: 无法初始化 Oracle 调用界面”
WIN7 64bit安装Oracle 10.2.0.1后,运行cmd-sqlplus / as sysdba会提示: C:\Users\Liu>sqlplus / as sysdba SP2-1 ...
- SQL Server 2012 Enterprise Edition安装过程详解(包含每一步设置的含义)
一.启动安装程序,点击“安装”选项卡,选择“全新SQL Server独立安装或向现有安装添加功能”.(首次安装数据库系统或向现有数据库系统添加功能,均选择此选项) 二.随后,安装程序进行“安装程序支持 ...
- iOS设计模式解析(六)代理模式
代理模式:为其他对象创建一个代理以控制对这个对象的访问 UML解释:客户端向Proxy发起一个Request()请求,Proxy对象会把这个Request转发给Proxy对象的RealSubject. ...
- mysql 蠕虫复制
INSERT into user_info(version,create_user_count,create_pc_count) select version,create_user_count,cr ...
- Centos查看端口占用情况和开启端口命令
Centos查看端口占用情况命令,比如查看80端口占用情况使用如下命令: lsof -i tcp:80 列出所有端口 netstat -ntlp 1.开启端口(以80端口为例)方法一: /sbin/i ...