C#入门

用Visual Studio创建项目

  • Visual Studio支持多种编程语言,创建项目时请选择C#语言。
  • Visual Studio中有各种开发模板,本书中将使用控制台模板和Windows窗口模板。

Hello World

  • 按行业惯例,学习编程编写的第一个程序就是Hello World,这意味着一个新的C#程序员诞生了,本程序将在界面上显示一串Hello World的文字。

    using System;
    
    namespace HelloWorld
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
            }
        }
    }
    
  • Main函数叫主函数,是整个程序的入口。所谓入口就是程序从这个函数的开执行第一个语句。执行到这个函数的程序就结束了。
  • 要打印的内容要用""引起来。
  • C#是大小写敏感的编程语言。所以Consoleconsole是两个完全不同的单词。

注释

  • //开头的语句是注释。
  • 注释相当于写代码时做的笔记,它不属于代码中的一部分,但可以提高代码的可读性。
  • 如果把一段正常的代码前加上//,那他就不再是代码,而变成了注释。可以用这种方法,让这段代码不执行。

    using System;
    
    namespace HelloWorld
    {
        class Program
        {
            // 主函数
            static void Main(string[] args)
            {
                // 在控制台上输出一段文字
                Console.WriteLine("Hello World!");
            }
        }
    }
    

有用的网站

results matching ""

    No results matching ""