C#学习

C#学习 - C#程序结构

一个C#程序主要包括以下结构:

  • 命名空间声明(Namespace declaration)
  • 一个class
  • class方法
  • class属性
  • 一个Main方法
  • 语句与表达式

我们以hello world程序为例:

using System;	// using关键字用于在程序中包含System命名空间,类似于C++中的include
namespace HelloWorldApplicatoin		// 命名空间声明
{
    class HelloWorld	// 类HelloWorld
    {
		static void Main(string[] args)		// 程序入口
        {
        	Console.WriteLine("Hello World");
            Console.ReadKey();
        }        
    }
}

输出结果:

Hello World

几点注意事项:

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

1. using 关键字

在C#中,using关键字有三种用途

  • 指定引用的命名空间

    using System.Windows.Forms;
    
  • 简化命名空间的层次表达形式

    using WinForm=System.Windows.Form;
    
  • 作为语句,定义一个范围

    Font font1 = new Font("Arial", 10.0f);
    using (font1) {...}
    

    当程序执行到}时,自动释放font1对象

---- suffer now and live the rest of your life as a champion ----
原文地址:https://www.cnblogs.com/popodynasty/p/14492000.html