摘要
我们做一个Hello world的输出后台控制器程序。
正文
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 | int |
interface | internal | is | lock | long | namespace | new |
null | object | operator | out | out | 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 |
partial | remove | select | set |
标识符
标识符是用于标识类,变量,函数或任何其他用户定义项的名称。C#中命名类的基本规则如下:
Main方法的要求
Main方法必需定义为static
Main方法首字母必须大写
返回值可以是void或int其它
命令行参数可选
namespace test1 //命名空间
{
class A //类名
{
static int Main(string[] arg) //方法入口
{
Console.WriteLine("您好,C#");//语法
return 0;
}
}
}传入一个参数
namespace test1 //命名空间
{
class A //类名
{
static int Main(string[] arg) //方法入口
{
Console.WriteLine(arg[0]);//输出参数,这里是一个数组
Console.WriteLine("您好,C#");//语法
return 0;
}
}
}注意:一个程序只能有一个Main入口方法。
注释
/*
* 创建者:张三
* 创建日期:2022-01-01
*/
namespace test1 //命名空间
{
#region"类"
class A //类名
{
///
/// 这个是方法入口
///
/// 传入参数
///
static int Main(string[] arg) //方法入口
{
Console.WriteLine(arg[0]);//输出参数,这里是一个数组
Console.WriteLine("您好,C#");//语法
return 0;
}
}
#endregion
}快捷键:
一段完整的程序
/*
* 创建者:张三
* 创建日期: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
}命名规范
字母大小写约定
| 留言与评论(共有 0 条评论) “” |