天天看點

c#&vb兩種語言文法的簡要介紹

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/chinahuyong/article/details/2882830

 c#&vb兩種語言文法的簡要介紹

1.變量聲名

C# 文法

int x;

String s;

String s1, s2;

Object o;

Object obj = new Object();

public String name;

VB文法

Dim x As Integer

Dim s As String

Dim s1, s2 As String

Dim o 'Implicitly Object

Dim obj As New Object()

Public name As String

2語句

C#:

Response.Write("中文c#技術站");

VB:

Response.Write("中文c#技術站")

3.注釋語句

//中文c#技術站

/*

歡迎通路

中文c#技術站

*/

'中文c#技術站

4.獲得URL 傳遞的變量

String s = Request.QueryString["Name"];

String value = Request.Cookies["key"];

Dim s, value As String

s = Request.QueryString("Name")

value = Request.Cookies("Key").Value

5.聲明屬性

public String name {

get {

...

return ...;

}

set {

... = value;

Public Property Name As String

Get

Return ...;

End Get

Set

... = Value;

End Set

End Property

6.數組

C#

String[] a = new String[3];

a[0] = "1";

a[1] = "2";

a[2] = "3";

//二維數組

String[][] a = new String[3][3];

a[0][0] = "1";

a[1][0] = "2";

a[2][0] = "3";

Dim a(3) As String

a(0) = "1"

a(1) = "2"

a(2) = "3"

Dim a(3,3) As String

a(0,0) = "1"

a(1,0) = "2"

a(2,0) = "3"

Dim a() As String

Dim a(,) As String

7變量初始化

String s = "Hello World";

int i = 1

double[] a = { 3.00, 4.00, 5.00 };

Dim s As String = "Hello World"

Dim i As Integer = 1

Dim a() As Double = { 3.00, 4.00, 5.00 }

8;判斷語句(If 語句)

if (Request.QueryString != null) {

If Not (Request.QueryString = Null)

End If

9.分支語句(case 語句)

switch (FirstName) {

case "John" :

break;

case "Paul" :

case "Ringo" :

Select (FirstName)

End Select

10 For循環語句

for (int i=0; i<3; i++)

a(i) = "test";

Dim I As Integer

For I = 0 To 2

a(I) = "test"

Next

11 While 循環

int i = 0;

while (i<3) {

Console.WriteLine(i.ToString());

i += 1;

I = 0

Do While I < 3

Console.WriteLine(I.ToString())

I = I + 1

Loop

12 字元串連接配接

String s1;

String s2 = "hello";

s2 += " world";

s1 = s2 + " !!!";

s2 = "hello"

s2 &= " world"

s1 = s2 & " !!!"

聲明事件

void MyButton_Click(Object sender,

EventArgs E) {

Sub MyButton_Click(Sender As Object,

E As EventArgs)

End Sub

13 聲明Object

MyObject obj = (MyObject)Session["Some Value"];

IMyObject iObj = obj

Dim bj As MyObject

Dim iObj As IMyObject

obj = Session("Some Value")

iObj = CType(obj, IMyObject)

14 資料類型轉換

int i = 3;

String s = i.ToString();

double d = Double.Parse(s);

Dim i As Integer

Dim d As Double

i = 3

s = i.ToString()

d = CDbl(s)

15 類的聲明和繼承

using System;

namespace MySpace {

public class Foo : Bar {

public Foo() { x = 4; }

public void Add(int x) { this.x += x; }

public int GetNum() { return x; }

Imports System

Namespace MySpace

Public Class Foo : Inherits Bar

Public Sub New()

MyBase.New()

x = 4

Public Sub Add(x As Integer)

Me.x = Me.x + x

Public Function GetNum() As Integer

Return x

End Function

End Class

End Namespace

16 聲明類的主函數

public class ConsoleCS {

public ConsoleCS() {

Console.WriteLine("Object Created");

public static void Main (String[] args) {

Console.WriteLine("Hello World");

ConsoleCS ccs = new ConsoleCS();

VB

Public Class ConsoleVB

Console.WriteLine("Object Created")

Public Shared Sub Main()

Console.WriteLine("Hello World")

Dim cvb As ConsoleVB

cvb = New ConsoleVB()

17 标準子產品

public class Module {

Public Module ConsoleVB

Public Sub Main()

End Module

繼續閱讀