当前位置:首页 > Windows程序 > 正文

VB.NET and C# 差异

2021-03-28 Windows程序

标签:

VB.NET   Program Structure   C#  
 
Imports System
Namespace Hello    Class HelloWorld       Overloads Shared Sub Main(ByVal args() As String)          Dim name As String = "VB.NET"
         ‘See if an argument was passed from the command line           If args.Length = 1 Then name = args(0)
          Console.WriteLine("Hello, " & name & "!")       End Sub    End Class End Namespace
  using System;
namespace Hello {    public class HelloWorld {       public static void Main(string[] args) {          string name = "C#";
         // See if an argument was passed from the command line          if (args.Length == 1)             name = args[0];
         Console.WriteLine("Hello, " + name + "!");       }    } }
 
VB.NET   Comments   C#  
 
‘ Single line only Rem Single line only  

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

 
VB.NET   Data Types   C#  
 

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 d As Single = 3.5 Dim i As Integer = CType(d, Integer)   ‘ set to 4 (Banker‘s rounding) i = CInt(d)  ‘ same result as CType i = Int(d)    ‘ 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 float d = 3.5f; int i = (int)d;   // set to 3  (truncates decimal)

 
VB.NET   Constants   C#  
 
Const MAX_STUDENTS As Integer = 25 

‘ Can set to a const or var; may be initialized in a constructor ReadOnly MIN_DIAMETER As Single = 4.93

  const int MAX_STUDENTS = 25; 

// Can set to a const or var; may be initialized in a constructor readonly float MIN_DIAMETER = 4.93f;

 
VB.NET   Enumerations   C#  
 
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.ToString & " is " & a)     ‘ Prints "Stop is 1"
Console.WriteLine(Status.Pass)     ‘ Prints 70 Console.WriteLine(Status.Pass.ToString())     ‘ 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((int) Status.Pass);    // Prints 70 Console.WriteLine(Status.Pass);      // Prints Pass
 
VB.NET   Operators   C#  
 

Comparison =  <  >  <=  >=  <>

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

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

Bitwise And   Or   Not   <<   >>

Logical AndAlso   OrElse   And   Or   Xor   Not

Note: AndAlso and OrElse perform short-circuit logical evaluations

String Concatenation &   +

 

Comparison ==  <  >  <=  >=  !=

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/69353.html