zoukankan      html  css  js  c++  java
  • C# 编程基础教程

    2015-04-02 Jenny摘录:

    C# 简介

    C# 是一个简单的、现代的、通用的、面向对象的编程语言,它是由微软(Microsoft)开发的。

    虽然 C# 的构想十分接近于传统高级语言 C 和 C++,是一门面向对象的编程语言,但是它与 Java 非常相似,有许多强大的编程功能,因此得到广大程序员的亲睐。

    下面列出 C# 一些重要的功能:

    • 布尔条件(Boolean Conditions)
    • 自动垃圾回收(Automatic Garbage Collection)
    • 标准库(Standard Library)
    • 组件版本(Assembly Versioning)
    • 属性(Properties)和事件(Events)
    • 委托(Delegates)和事件管理(Events Management)
    • 易于使用的泛型(Generics)
    • 索引器(Indexers)
    • 条件编译(Conditional Compilation)
    • 简单的多线程(Multithreading)
    • LINQ 和 Lambda 表达式
    • 集成 Windows

    一个 C# 程序主要包括以下部分:

    • 命名空间声明(Namespace declaration)
    • 一个 class
    • Class 方法
    • Class 属性
    • 一个 Main 方法
    • 语句(Statements)& 表达式(Expressions)
    • 注释

    让我们看一个可以打印出 "Hello World" 的简单的代码:

    using System;
    namespace HelloWorldApplication
    {
       class HelloWorld
       {
          static void Main(string[] args)
          {
             /* 我的第一个 C# 程序*/
             Console.WriteLine("Hello World");
             Console.ReadKey();
          }
       }
    }
    上面的编译和执行的结果是:

    Hello World

    让我们看一下上面程序的各个部分:

    • 程序的第一行 using System; - using 关键字用于在程序中包含 System 命名空间。 一个程序一般有多个 using 语句。
    • 下一行是 namespace 声明。一个 namespace 是一系列的类。HelloWorldApplication 命名空间包含了类 HelloWorld
    • 下一行是 class 声明。类 HelloWorld 包含了程序使用的数据和方法声明。类一般包含多个方法。方法定义了类的行为。在这里,HelloWorld 类只有一个 Main 方法。
    • 下一行定义了 Main 方法,是所有 C# 程序的 入口点Main 方法说明当执行时 类将做什么动作。
    • 下一行 /*...*/ 将会被编译器忽略,且它会在程序中添加额外的 注释
    • Main 方法通过语句 Console.WriteLine("Hello World"); 指定了它的行为。

      WriteLine 是一个定义在 System 命名空间中的 Console 类的一个方法。该语句会在屏幕上显示消息 "Hello, World!"。

    • 最后一行 Console.ReadKey(); 是针对 VS.NET 用户的。这使得程序会等待一个按键的动作,防止程序从 Visual Studio .NET 启动时屏幕会快速运行并关闭。

    以下几点值得注意:

    • C# 是大小写敏感的。
    • 所有的语句和表达式必须以分号(;)结尾。
    • 程序的执行从 Main 方法开始。
    • 与 Java 不同的是,文件名可以不同于类的名称。

    C# 基本语法

    C# 是一种面向对象的编程语言。在面向对象的程序设计方法中,程序由各种相互交互的对象组成。相同种类的对象通常具有相同的类型,或者说,是在相同的 class 中。

    例如,以 Rectangle(矩形)对象为例。它具有 length 和 width 属性。根据设计,它可能需要接受这些属性值、计算面积和显示细节。

    让我们来看看一个 Rectangle(矩形)类的实现,并借此讨论 C# 的基本语法:

    using System;
    namespace RectangleApplication
    {
        class Rectangle
        {
            // 成员变量
            double length;
            double width;
            public void Acceptdetails()
            {
                length = 6.5;    
                width = 4.5;
            }
            public double GetArea()
            {
                return length * width;
            }
            public void Display()
            {
                Console.WriteLine("Length: {0}", length);
                Console.WriteLine("Width: {0}", width);
                Console.WriteLine("Area: {0}", GetArea());
            }
        }
        
        class ExecuteRectangle
        {
            static void Main(string[] args)
            {
                Rectangle r = new Rectangle();
                r.Acceptdetails();
                r.Display();
                Console.ReadLine();
            }
        }
    }

    当上面的代码被编译和执行时,它会产生下列结果:

    Length: 6.5
    Width: 4.5
    Area: 29.25

    using 关键字

    在任何 C# 程序中的第一条语句都是:

    using System;

    using 关键字用于在程序中包含命名空间。一个程序可以包含多个 using 语句。

    class 关键字

    class 关键字用于声明一个类。

    C#中的注释

    注释是用于解释代码。编译器会忽略注释的条目。在 C# 程序中,多行注释以 /* 开始,并以字符 */ 终止,如下所示:

    /* This program demonstrates
    The basic syntax of C# programming 
    Language */
    

    单行注释是用 '//' 符号表示。例如:

    }//end class Rectangle   

    成员变量

    变量是类的属性或数据成员,用于存储数据。在上面的程序中,Rectangle 类有两个成员变量,名为 length 和 width

    成员函数

    函数是一系列执行指定任务的语句。类的成员函数是在类内声明的。我们举例的类 Rectangle 包含了三个成员函数:AcceptDetailsGetArea 和 Display

    实例化一个类

    在上面的程序中,类 ExecuteRectangle 是一个包含 Main() 方法和实例化 Rectangle 类的类。

    标识符

    标识符是用来识别类、变量、函数或任何其它用户定义的项目。在 C# 中,类的命名必须遵循如下基本规则:

    • 标识符必须以字母开头,后面可以跟一系列的字母、数字( 0 - 9 )或下划线( _ )。标识符中的第一个字符不能是数字。
    • 标识符必须不包含任何嵌入的空格或符号,比如 ? - +! @ # % ^ & * ( ) [ ] { } . ; : " ' / 。但是,可以使用下划线( _ )。
    • 标识符不能是 C# 关键字。

    C#关键字

    关键字是 C# 编译器预定义的保留字。这些关键字不能用作标识符,但是,如果您想使用这些关键字作为标识符,可以在关键字前面加上 @ 字符作为前缀。

    在 C# 中,有些标识符在代码的上下文中有特殊的意义,如 getset,这些被称为上下文关键字(contextual keywords)。

    下表列出了 C# 中的保留关键字(Reserved Keywords)和上下文关键字(Contextual Keywords): 

    保留关键字
    abstract as base bool break byte case
    catch char checked class const continue decimal
    default delegate do double else enum event
    explicit extern false finally fixed float for
    foreach goto if implicit in in (generic modifier) int
    interface internal is lock long namespace new
    null object operator out out
    (generic modifier)
    override params
    private protected public readonly ref return sbyte
    sealed short sizeof stackalloc static string struct
    switch this throw true try typeof uint
    ulong unchecked unsafe ushort using virtual void
    volatile while          
    上下文关键字
    add alias ascending descending dynamic from get
    global group into join let orderby partial
    (type)
    partial
    (method)
    remove select set      

     

     

     

     

     

     

     

     

     

     

     

     

     

     

      

     

    Nothing is impossible, if you set your mind to it !
  • 相关阅读:
    Python 必备神器
    python 常用库
    Sublime Text3 配置 Python2 Python3
    Python JSON
    Sublime Text3 3143 注册码
    EFCode First 导航属性
    EF Code First:实体映射,数据迁移,重构(1)
    Entity Framework 复杂类型
    EF 7 Code First
    EF Code First 导航属性 与外键
  • 原文地址:https://www.cnblogs.com/penny1141/p/4386936.html
Copyright © 2011-2022 走看看