C# 与 VB.NET 对比
2008-06-20 15:30 Anders Cui 阅读(2360) 评论(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 |
using System; |
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 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 Action {Start, Stop, Rewind, Forward}; |
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.
|
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
// 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}
|
int[] nums = {1, 2, 3}; 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 ' Optional parameters must be listed last and must have a default value |
// 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. */ |
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 ' My birthday: Oct 12, 1973 ' Mutable string |
Escape sequences // Chars // String literal // String comparison Console.WriteLine(mascot.Substring(2, 3)); // Prints "son" // String matching // 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 |
class 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");
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 Dim stu As StudentRecord = New StudentRecord("Bob", 3.5) |
struct StudentRecord { StudentRecord stu = new StudentRecord("Bob", 3.5f); |
2.18 Properties
VB.NET |
C#.NET |
Private _size As Integer foo.Size += 1 |
private int _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, AddressOf My_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#
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? ") |
Console.Write("What's your name? ");
|
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 |
3.0 References
出处:http://anderslly.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。