4.编写第一个C#程序

摘要

我们做一个Hello world的输出后台控制器程序。

正文

C#创建程序顺序

新建项目→编写代码→调试或运行,系统会自动在你创建的项目上加一个解决方案层。

4.编写第一个C#程序

在.Net 6时,没有Main入口方法了,Program自动增加了一行Console.WriteLine("Hello, World!");

Console这个类

Console.Write 表示向控制台直接写入字符串,不进行换行,可继续接着前面的字符写入。

Console.WriteLine 表示向控制台写入字符串后换行。

Console.Read 表示从控制台读取字符串,不换行。

Console.ReadLine 表示从控制台读取字符串后进行换行。

Console.ReadKey 获取用户按下的下一个字符或功能键,按下的键显示在控制台窗口中。

Console.Beep 通过控制台扬声器播放提示音。

Console.Clear 清除控制台缓冲区和相应的控制台窗口的显示信息。

Console.BackgroundColor = ConsoleColor.Blue; //设置背景色

Console.ForegroundColor = ConsoleColor.White; //设置前景色,即字体颜色

Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Hello, World!");

命名空间

C#程序中的一种代码组织形式,主要用来标识类的可见范围。

命名空间类似于类的文件夹,仅系统预设的类就有数千乃至上万。使用命名空间可以起到文件夹的作用,也是就把类归类整理。

namespace test1
{
    class Program
    {

    }
}

namespace text2
{
    class Program
    {
        test1.Program p = new test1.Program();
    }
}

使用引用命名空间 using test1;

namespace test1
{
    class A
    {

    }
}

namespace text2
{
    using test1;
    class B
    {
        A p = new A();
    }
}

命名空间可以嵌套

namespace test1
{
    class A
    {

    }

    namespace test2
    {
        class B
        {
            A b=new A();
        }
    }
}

使用别名引用命名空间

namespace test1
{
    class A
    {

    }
}
namespace test2
{
    using T1 = test1;
    class B
    {
        T1.A a = new T1.A();
    }
}

什么是类

一种数据结构,存储数据成员、方法成员和其它类等内容,便于调用。这个是面向对像的核心!

class A
{

}
namespace test1 //命名空间
{
    class A //类名
    {
        static void Main() //方法入口
        {
            Console.WriteLine("您好,C#");//语法
        }
    }
}

关键字

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

保留关键字







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



标识符

标识符是用于标识类,变量,函数或任何其他用户定义项的名称。C#中命名类的基本规则如下:

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

Main方法的要求

Main方法必需定义为static

Main方法首字母必须大写

返回值可以是void或int其它

命令行参数可选

namespace test1 //命名空间
{
    class A //类名
    {
        static int Main(string[] arg) //方法入口
        {
            Console.WriteLine("您好,C#");//语法
            return 0;
        }
    }
}

传入一个参数

4.编写第一个C#程序

namespace test1 //命名空间
{
    class A //类名
    {
        static int Main(string[] arg) //方法入口
        {
            Console.WriteLine(arg[0]);//输出参数,这里是一个数组
            Console.WriteLine("您好,C#");//语法
            return 0;
        }
    }
}

注意:一个程序只能有一个Main入口方法。

注释

  • // 单行注释
  • /**/ 块注释
  • ///说明注释,注释以后可以自动生成说明文档档
  • #region 折叠注释,可以将代码折叠 #endregion 只是#region 所在行后面的文字是注释文字,而其它的#region和#endregion之内的行代码是有效的,仅仅起折叠作用
/*
 * 创建者:张三
 * 创建日期:2022-01-01
 */
namespace test1 //命名空间
{
    #region"类"
    class A //类名
    {
        /// 
        /// 这个是方法入口
        /// 
        /// 传入参数
        /// 
        static int Main(string[] arg) //方法入口
        {
            Console.WriteLine(arg[0]);//输出参数,这里是一个数组
            Console.WriteLine("您好,C#");//语法
            return 0;
        }
    }
    #endregion
}

快捷键:

  1. 注释快捷键: Ctrl + K + C
  2. 取消注释快捷键: Ctrl + K + U

一段完整的程序

/*
 * 创建者:张三
 * 创建日期:2022-01-01
 */
namespace test1 //命名空间
{
    #region"类"
    class A //类名
    {
        /// 
        /// 程序入口
        /// 
        /// 传入参数
        static void Main(string[] arg) //方法入口
        {
            Console.WriteLine("----------------------------");
            Console.WriteLine("|            PLC           |");
            Console.WriteLine(" -------------------------- ");
            Console.WriteLine("|                          |");
            Console.WriteLine("|                          |");
            Console.WriteLine("----------------------------");
        }
    }
    #endregion
}

命名规范

字母大小写约定

  • Pascal风格:将标识符的首字母和后面连接的每个单词的首字母都大写。 如:Name,GetName
  • Camel风格:标识符的首字母小写,而每个后面连接的单词的首字母都大写 userId,getName
  • 项目名:公司名.产品名 Idiosoft.Mes
  • 命名空间:公司名或产品名
  • 接口:大写"I"开头,像IRun
  • 类名:一定要休现功能与操作的意义,像用户类,User,操作类,Operation
  • 方法名:休现出这个方法的意思GetName,当然现在有一种更简单的直接写Name这样来做。
  • 私有的成员变量:前缀写成"_"
  • 其它变量:小写字母
  • ORM实体类:用小写字母,这个在设计数据库表时也可以用这个规则,如果是两个单词用"_"隔离,像name,created_dated
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章