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

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3693 更新时间:2009/4/23 19:01:40
 
  c += 1
Loop Until c = 10

''''  Array or collection looping
Dim names As String() = {"Fred", "Sue", "Barney"}
For Each s As String In names
  Console.WriteLine(s)
Next

Pre-test Loops:  

// no "until" keyword
while (i < 10)
  i++;

for (i = 2; i < = 10; i += 2)
  Console.WriteLine(i);



Post-test Loop:

do
  i++;
while (i < 10);



// Array or collection looping

string[] names = {"Fred", "Sue", "Barney"};
foreach (string s in names)
  Console.WriteLine(s);

Arrays

Dim nums() As Integer = {1, 2, 3} 
For i As Integer = 0 To nums.Length - 1
  Console.WriteLine(nums(i))
Next

'''' 4 is the index of the last element, so it holds 5 elements
Dim names(4) As String
names(0) = "David"
names(5) = "Bobby"  '''' Throws System.IndexOutOfRangeException

'''' Resize the array, keeping the existing values (Preserve is optional)
ReDim Preserve names(6)



Dim twoD(rows-1, cols-1) As Single
twoD(2, 0) = 4.5

Dim jagged()() As Integer = { _
  New Integer(4) {}, New Integer(1) {}, New Integer(2) {} }
jagged(0)(4) = 5

int[] nums = {1, 2, 3};
for (int i = 0; i < nums.Length; i++)
  Console.WriteLine(nums[i]);


// 5 is the size of the array
string[] names = new string[5];
names[0] = "David";
names[5] = "Bobby";   // Throws System.IndexOutOfRangeException


// C# doesn''''t can''''t dynamically resize an array.  Just copy into new array.
string[] names2 = new string[7];
Array.Copy(names, names2, names.Length);   // or names.CopyTo(names2, 0); 

float[,] twoD = new float[rows, cols];
twoD[2,0] = 4.5f; 

int[][] jagged = new int[3][] {
  new int[5], new int[2], new int[3] };
jagged[0][4] = 5;

Functions

'''' Pass by value (in, default), reference (in/out), and reference (out) 
Sub TestFunc(ByVal x As Integer, ByRef y As Integer, ByRef z As Integer)
  x += 1
  y += 1
  z = 5
End Sub

Dim a = 1, b = 1, c As Integer   '''' c set to zero by default 
TestFunc(a, b, c)
Console.WriteLine("{0} {1} {2}", a, b, c)   '''' 1 2 5

'''' Accept variable number of arguments
Function Sum(ByVal ParamArray nums As Integer()) As Integer
  Sum = 0 
  For Each i As Integer In nums
    Sum += i
  Next
End Function   '''' Or use Return statement like C#

Dim total As Integer = Sum(4, 3, 2, 1)   '''' returns 10

'''' Optional parameters must be listed last and must have a default value
Sub SayHello(ByVal name As String, Optional ByVal prefix As String = "")
  Console.WriteLine("Greetings, " & prefix & " " & name)
End Sub

SayHello("Strangelove", "Dr.")
SayHello("Madonna")

// 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
TestFunc(a, ref b, out c);
Console.WriteLine("{0} {1} {2}", a, b, c);  // 1 2 5

// Accept variable number of arguments
int Sum(params int[] nums) {
  int sum = 0;
  foreach (int i in nums)
    sum += i;
  return sum;
}

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, string prefix) {
  Console.WriteLine("Greetings, " + prefix + " " + name);


void SayHello(string name) {
  SayHello(name, "");
}

Exception Handling

'''' Deprecated unstructured error handling
On Error GoTo MyErrorHandler
...
MyErrorHandler: Console.WriteLine(Err.Description)

Dim ex As New Exception("Something is really wrong.")
Throw  ex 

Try 
  y = 0
  x = 10 / y
Catch ex As Exception When y = 0 '''' Argument and When is optional
  Console.WriteLine(ex.Message)
Finally
  Beep()
End Try





Exception up = new Exception("Something is really wrong.");
throw up;  // ha ha

try
  y = 0;
  x = 10 / y;
}
catch (Exception ex) {   // Argument is optional, no "When" keyword 
  Console.WriteLine(ex.Message);
}
finally {
  // Must use unmanaged MessageBeep API function to beep
}

Namespaces

Namespace Harding.Compsci.Graphics 
  ...
End Namespace

'''' or

Namespace Harding
  Namespace Compsci
    Namespace Graphics 
      ...
    End Namespace
  End Namespace
End Namespace

Import Harding.Compsci.Graphics

namespace Harding.Compsci.Graphics {
  ...
}

// or

namespace Harding {
  namespace Compsci {
    namespace Graphics {
      ...
    }
  }
}

using Harding.Compsci.Graphics;

Classes / Interfaces

Accessibility keywords
Public
Private
Friend                   
Protected
Protected Friend
Shared

'''' Inheritance
Class FootballGame
  Inherits Competition
  ...
End Class 

'''' Interface definition
Interface IAlarmClock 
  ...
End Interface

// Extending an interface 
Interface IAlarmClock
  Inherits IClock
  ...
End Interface

// Interface implementation
Class WristWatch 
  Implements IAlarmClock, ITimer 
   ...
End Class 

Accessibility keywords
public
private
internal
protected
protected internal
static

// Inheritance
class FootballGame : Competition {
  ...
}


// Interface definition
interface IAlarmClock {
  ...
}

// Extending an interface 
interface IAlarmClock : IClock {
  ...
}


// Interface implementation
class WristWatch : IAlarmClock, ITimer {
   ...
}

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 () 
&

上一页  [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……
    咸宁网络警察报警平台