当前位置:网站首页>1,C#的基本结构
1,C#的基本结构
2022-08-09 05:14:00 【真的没事鸭】
C#程序结构
- 命名空间声明(Namespace declaration)
- 一个 class(类)
- Class 方法
- Class 属性
- 一个 Main 方法
- 语句(Statements)& 表达式(Expressions)
- 注释
下面以一个简单的代码为例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("HelloWorld");
Console.ReadKey();
}
}
}
这个程序执行之后的结果是“HelloWorld”
分析
前五行用using关键字引入命名空间,引用之后我们可以直接使用命名空间中的类型,比如第一行我们引用了System的命名控件,其中定义了类Console,我们可以直接用Console类的方法
Console.WriteLine(“Hello World”)
如果我们不用using的话,我们还要写全限定名称
System.Console.WriteLine(“Hello World”)
在任何C#程序中的第一条语句都是
using System;
然后是namespace,namespace后面跟命名空间的名称,它是用来定义命名空间的。上面我们跟了ConsoleApp1,这样就类似于System。如果我们要在别的类中使用Program类型,我们需要使用它的完全限定名
Class Program1
{
ConsoleApp1.Program program=new ConsoleApp1.Program();
}
但是我们用using ConsoleApp1引用了之后就可以不用写完全限定名
Class Program1
{
Program program=new Program();
}
然后下一行是Class声明,声明的这个类中包含了程序使用的数据和方法。类一般有多个方法。方法定义了类的行为。Class关键字用于声明一个类
下一行定义了Main方法,Main方法是所有程序的入口,程序的执行从main方法开始
下一行使用了Console类的一个方法WriteLine,该方法用来 输出
下一行Console.ReadKey(),防止程序启动后快速关闭
还有就是注释,和c和c++一样,//是单行注释,/*~*/是多行注释
C#的标识符
- 标识符必须以字母、下划线或 @ 开头,后面可以跟一系列的字母、数字( 0 - 9 )、下 划线( _ )、@。
- 标识符中的第一个字符不能是数字。
- 标识符必须不包含任何嵌入的空格或符号,比如 ? - +! # % ^ & * ( ) [ ] { } . ; : " ' / \。
- 标识符不能是 C# 关键字。除非它们有一个 @ 前缀。 例如,@if 是有效的标识符, 但 if 不是,因为 if 是关键字。
- 标识符必须区分大小写。大写字母和小写字母被认为是不同的字母。
- 不能与C#的类库名称相同。
C#关键字
如有错漏之处,敬请指正!
边栏推荐
猜你喜欢
2022-08-08 第四小组 修身课 学习笔记(every day)
How to trim svg and compress
TP6的安装与测试
【零基础玩转BLDC系列】无刷直流电机闭环控制与软件架构
Quantitative Genetics Heritability Calculation 1: Parent-Child Regression Method
What is it like to work at Kuaishou?
【MLT】MLT多媒体框架生产消费架构解析(二)
matlab simulink 温度控制时延系统 模糊pid和smith控制
剑指Offer-二叉树路径问题总结
C Advanced - Program Compilation (Preprocessing) + Linking
随机推荐
华为鲲鹏生态培训试题
JDBC_PreparedStatement预编译对象
Hhhhgffsb
dsafasfdasfasf
Software testing method is introduced in detail
匿名共享内存 ashmem
Why do enterprises need business intelligence BI in the digital age
后台登录模块以及验证码登录
什么是ReFi?
[Developers must see] [push kit] Collection of typical problems of push service service 2
【HMS core】【ML kit】Machine Learning Service FAQ
The development trend of software testing
数组 冒泡排序
图解LeetCode——761. 特殊的二进制序列(难度:困难)
PWM输出模块PCA9685
ELTEK电源维修SMPS5000SIL整流器模块故障分析及特点
Oracle01-安装与卸载
机器人大赛总结
The request was rejected because the URL contained a potentially malicious String “//“
【luogu U142356】Suffix of the Brave (SA) (Chairman Tree) (2 points)