VB.NET |
C# |
||||||||||
Comments | |||||||||||
' Single line only Rem Single line only |
// Single line |
||||||||||
Data Types | |||||||||||
Value Types
Reference Types
Dim x As Integer
' Type conversion |
Value Types
Reference Types
int x;
|
||||||||||
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
Assignment
Bitwise
Logical Note: AndAlso and OrElse are for short-circuiting logical evaluations
String Concatenation |
Comparison
Arithmetic
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"
' Use : to put two commands on same line
' or to break up any long single command use _
'If x > 5 Then
Select Case color ' Must be a primitive data type |
greeting = age < 20 ? "What's up?" : "Hello";
if (x != 100) { // Multiple statements must be enclosed in {} No need for _ or : since ; is used to terminate each statement.
|
||||||||||
Loops | |||||||||||
' Array or collection looping |
Pre-test Loops:
// no "until" keyword
|
||||||||||
Arrays | |||||||||||
Dim nums() As Integer = {1, 2, 3}
|
int[] nums = {1, 2, 3};
float[,] twoD = new float[rows, cols];
int[][] jagged = new int[3][] { |
||||||||||
Functions | |||||||||||
' 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) void TestFunc(int x, ref int y, out int z) { x++; y++; z = 5; }
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. */ |
||||||||||
Exception Handling | |||||||||||
' Deprecated unstructured error handling
Try |
try { |
||||||||||
Namespaces | |||||||||||
Namespace Harding.Compsci.Graphics ' or
Namespace Harding Import Harding.Compsci.Graphics |
namespace Harding.Compsci.Graphics { // or
namespace Harding { using Harding.Compsci.Graphics; |
||||||||||
Classes / Interfaces | |||||||||||
Accessibility keywords
' Inheritance
' Interface definition
// Extending an interface
// Interface implementation |
Accessibility keywords
// Inheritance
// Extending an interface
|
||||||||||
Constructors / Destructors | |||||||||||
Class SuperHero Private _powerLevel As Integer Public Sub New () _powerLevel = 0 End Sub Public Sub New (ByVal powerLevel As Integer) Me._powerLevel = powerLevel End Sub Protected Overrides Sub Finalize () ' Desctructor code to free unmanaged resources MyBase.Finalize() End Sub End Class |
class SuperHero { |
||||||||||
Objects | |||||||||||
Dim hero As SuperHero = New SuperHero
Dim hero2 As SuperHero = hero ' Both refer to same object hero = Nothing ' Free the object
If hero Is Nothing Then _
Dim obj As Object = New SuperHero |
SuperHero hero = new SuperHero();
hero.Defend("Laura Jones");
hero = null ; // Free the object
if (hero == null)
|
||||||||||
Structs | |||||||||||
Structure StudentRecord
Dim stu As StudentRecord = New StudentRecord("Bob", 3.5) |
struct StudentRecord { public string name; public float gpa; public StudentRecord(string name, float gpa) { this.name = name; this.gpa = gpa; } }
StudentRecord stu = new StudentRecord("Bob", 3.5f); |
||||||||||
Properties | |||||||||||
Private _size As Integer foo.Size += 1 |
private int _size; public int Size { get { return _size; } set { if (value < 0) _size = 0; else _size = value; } }
|
||||||||||
Delegates / Events | |||||||||||
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) { |
||||||||||
Console I/O | |||||||||||
Special character constants
Console.Write("What's your name? ") |
Escape sequences
Convert.ToChar(65) // Returns 'A' - equivalent to Chr(num) in VB
Console.Write("What's your name? ");
|
||||||||||
File I/O | |||||||||||
Imports System.IO
Dim writer As StreamWriter = File.CreateText("c:\myfile.txt")
Dim reader As StreamReader = File.OpenText("c:\myfile.txt")
Dim str As String = "Text data"
Dim binReader As New BinaryReader (File.OpenRead("c:\myfile.dat")) |
using System.IO;
StreamWriter writer = File.CreateText("c:\\myfile.txt");
StreamReader reader = File.OpenText("c:\\myfile.txt");
string str = "Text data";
BinaryReader binReader = new BinaryReader(File.OpenRead("c:\\myfile.dat")); |
-
c#.NET和VB.NET语法的比较
出处:https://www.cnblogs.com/lify0407/archive/2007/08/01/838589.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式