zoukankan      html  css  js  c++  java
  • C# 语法汇总

    语言支持

    Microsoft .NET Platform 目前提供对以下三种语言的内置支持:C#、Visual Basic 和 JScript。

    本教程中的练习和代码示例展示如何使用 C#、Visual Basic 和 JScript 生成 .NET 应用程序。有关其他语言语法的信息,请参考 .NET 框架 SDK 的完整文档。

    提供下表以帮助您理解本教程中的代码示例以及三种语言之间的差异:

    变量声明

    int x;
                String s;
                String s1, s2;
                Object o;
                Object obj = new Object();
                public String name;
                
    C# VB JScript  

    语句

    Response.Write("foo");
                
    C# VB JScript  

    注释

    ' This is a comment
                ' This
                ' is
                ' a
                ' multiline
                ' comment
                
    C# VB JScript  

    访问索引属性

    String s = Request.QueryString["Name"];
                String value = Request.Cookies["key"];
                
    C# VB JScript  

    声明索引属性

    // Default Indexed Property
                public String this[String name] {
                get {
                return (String) lookuptable[name];
                }
                }
                
    C# VB JScript  

    声明简单属性

    public String name {
                get {
                ...
                return ...;
                }
                set {
                ... = value;
                }
                }
                
    C# VB JScript  

    声明和使用枚举

    // Declare the Enumeration
                public enum MessageSize {
                Small = 0,
                Medium = 1,
                Large = 2
                }
                // Create a Field or Property
                public MessageSize msgsize;
                // Assign to the property using the Enumeration values
                msgsize = Small;
                
    C# VB JScript  

    枚举集合

    foreach ( String s in coll ) {
                ...
                }
                
    C# VB JScript  

    声明和使用方法

    // Declare a void return function
                void voidfunction() {
                ...
                }
                // Declare a function that returns a value
                String stringfunction() {
                ...
                return (String) val;
                }
                // Declare a function that takes and returns values
                String parmfunction(String a, String b) {
                ...
                return (String) (a + b);
                }
                // Use the Functions
                voidfunction();
                String s1 = stringfunction();
                String s2 = parmfunction("Hello", "World!");
                
    C# VB JScript  

    自定义属性

    // Stand-alone attribute
                [STAThread]
                // Attribute with parameters
                [DllImport("ADVAPI32.DLL")]
                // Attribute with named parameters
                [DllImport("KERNEL32.DLL", CharSet=CharSet.Auto)]
                
    C# VB JScript  

    数组

        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";
                
    C# VB JScript  

    初始化

    String s = "Hello World";
                int i = 1;
                double[] a =  { 3.00, 4.00, 5.00 };
                
    C# VB JScript  

    If 语句

    if (Request.QueryString != null) {
                ...
                }
                
    C# VB JScript  

    Case 语句

    switch (FirstName) {
                case "John" :
                ...
                break;
                case "Paul" :
                ...
                break;
                case "Ringo" :
                ...
                break;
                default:
                ...
                break;
                }
                
    C# VB JScript  

    For 循环

    for (int i=0; i<3; i++)
                a(i) = "test";
                
    C# VB JScript  

    While 循环

    int i = 0;
                while (i<3) {
                Console.WriteLine(i.ToString());
                i += 1;
                }
                
    C# VB JScript  

    异常处理

    try {
                // Code that throws exceptions
                } catch(OverflowException e) {
                // Catch a specific exception
                } catch(Exception e) {
                // Catch the generic exceptions
                } finally {
                // Execute some cleanup code
                }
                
    C# VB JScript  

    字符串连接

    // Using Strings
                String s1;
                String s2 = "hello";
                s2 += " world";
                s1 = s2 + " !!!";
                // Using StringBuilder class for performance
                StringBuilder s3 = new StringBuilder();
                s3.Append("hello");
                s3.Append(" world");
                s3.Append(" !!!");
                
    C# VB JScript  

    事件处理程序委托

    void MyButton_Click(Object sender,
                EventArgs E) {
                ...
                }
                
    C# VB JScript  

    声明事件

    // Create a public event
                public event EventHandler MyEvent;
                // Create a method for firing the event
                protected void OnMyEvent(EventArgs e) {
                MyEvent(this, e);
                }
                
    C# VB JScript  

    向事件添加事件处理程序或从事件移除事件处理程序

    Control.Change += new EventHandler(this.ChangeEventHandler);
                Control.Change -= new EventHandler(this.ChangeEventHandler);
                
    C# VB JScript  

    强制类型转换

    MyObject obj = (MyObject)Session["Some Value"];
                IMyObject iObj = obj;
                
    C# VB JScript  

    转换

    int i = 3;
                String s = i.ToString();
                double d = Double.Parse(s);
                
    C# VB JScript  

    带继承的类定义

    using System;
                namespace MySpace {
                public class Foo : Bar {
                int x;
                public Foo() { x = 4; }
                public void Add(int x) { this.x += x; }
                override public int GetNum() { return x; }
                }
                }
                // csc /out:librarycs.dll /t:library
                // library.cs
                
    C# VB JScript  

    实现接口

    public class MyClass : IEnumerable {
                ...
                IEnumerator IEnumerable.GetEnumerator() {
                ...
                }
                }
                
    C# VB JScript  

    带 Main 方法的类定义

    using System;
                public class ConsoleCS {
                public ConsoleCS() {
                Console.WriteLine("Object Created");
                }
                public static void Main (String[] args) {
                Console.WriteLine("Hello World");
                ConsoleCS ccs = new ConsoleCS();
                }
                }
                // csc /out:consolecs.exe /t:exe console.cs
                
    C# VB JScript  

    标准模块

    using System;
                public class Module {
                public static void Main (String[] args) {
                Console.WriteLine("Hello World");
                }
                }
                // csc /out:consolecs.exe /t:exe console.cs
                
    C# VB JScript  


  • 相关阅读:
    Java
    Leetcode 计划
    Java虚拟机
    浅谈 MVP in Android
    【PAT】B1074 宇宙无敌加法器(20 分)
    【PAT】B1076 Wifi密码(15 分)
    【PAT】B1075 链表元素分类(25 分)
    【PAT】B1077 互评成绩计算(20 分)
    【PAT】B1078 字符串压缩与解压(20 分)
    【PAT】B1079 延迟的回文数(20 分)
  • 原文地址:https://www.cnblogs.com/gfwei/p/560306.html
Copyright © 2011-2022 走看看