基本语句
C# 是一种面向对象的编程语言。在面向对象的程序设计方法中,程序由各种相互交互的对象组成。相同种类的对象通常具有相同的类型,或者说,是在相同的
class 中。
自己按照书上动手写了一个求矩形面积的小代码,写上一些自己的想法
using System;
namespace 基本语法 //为项目工程的名字
{
class REC //随便命名
{
double length;//类的变量
double width;
public void init() //类的函数
{
length = 4.5;
width = 3.5;
}
public double Area() //面积
{
return length * width;
}
public void dispay() //输出
{
Console.WriteLine("length: {0}", length); //这里面的 {0} 用于打印出 ,后的值,可为int char double float型等
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", Area());
}
}
class PUT //这里也可以用一个类,把这类去掉也可以
{
static void Main(string[] args)
{
REC eC = new REC();
eC.init();
eC.Area();
eC.dispay ();
}
}
}标识符
标识符是用来识别类、变量、函数或任何其它用户定义的项目。在 C# 中,类的命名必须遵循如下基本规则:
- 标识符必须以字母、下划线或 @ 开头,后面可以跟一系列的字母、数字( 0 - 9 )、下划线( _ )、@。
- 标识符中的第一个字符不能是数字。
- 标识符必须不包含任何嵌入的空格或符号,比如 ? - +! # % ^ & * ( ) [ ] { } . ; : " ' / \。
- 标识符不能是 C# 关键字。除非它们有一个 @ 前缀。 例如,@if 是有效的标识符,但 if 不是,因为 if 是关键字。
- 标识符必须区分大小写。大写字母和小写字母被认为是不同的字母。
- 不能与C#的类库名称相同。
C# 关键字
关键字是 C# 编译器预定义的保留字。这些关键字不能用作标识符,但是,如果您想使用这些关键字作为标识符,可以在关键字前面加上 @ 字符作为前缀。
在 C# 中,有些标识符在代码的上下文中有特殊的意义,如 get 和 set,这些被称为上下文关键字(contextual keywords)。
下表列出了 C# 中的保留关键字(Reserved Keywords)和上下文关键字(Contextual Keywords):
| 保留关键字 | ||||||
| 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#的基本语句,包括面向对象的概念、类的定义与使用,并详细解释了标识符的命名规则及C#的关键字。
482

被折叠的 条评论
为什么被折叠?



