浅学C#(4)——总体框架、关键字、命名规则、代码规范

版权声明:转载请注明出处 https://blog.csdn.net/le_17_4_6/article/details/86636826

总体框架

Hiker.cs                      类名不一定等于文件名
using System; //每一个程序必须在开头使用这一语句
public sealed class HitchHiker 
{ 
    public static void Main()//程序从Main开始执行
    { 
        int result; 
        result = 9 * 6; 
        int thirteen; 
        thirteen = 13; 
        Console.Write(result / thirteen); //输出函数
        Console.Write(result % thirteen); 
    } 
} 
//上面各语句的具体用法以后会介绍 
/* 这个程序用来 
 * 演示C#的总体框架
 */ 

注意:上面的程序中,符号//表示注释,在//后面的同一行上的内容是注释;
/*和*/ 之间的内容都是注释
你可以在windows的命令行提示符下键入:csc Hiker.cs
进行编译产生可执行文件Hiker.exe
然后在windows的命令行提示符下键入:Hiker,你就可以看到在屏幕上显示42
(注:你必须装有.net framework)

和Java不一样,C#源文件名不一定要和C#源文件中包含的类名相同。C#对大小写敏感,所以Main的首字母为大写的M。
你可以定义一个返回值为int的Main函数,当返回值为0时表示成功:

public static int Main() { ... return 0; }
你也可以定义Main函数的返回值为void:
public static void Main() { ... }
你还可以定义Main函数接收一个string数组:
public static void Main(string[] args)
{
	foreach (string args in args) {
		System.Console.WriteLine(arg);
	}
}

程序中的Main函数必须为static。

标识符

标识符起名的规则:

  • 局部变量、局部常量、非公有实例域、函数参数使用camelCase规则;其他类型的标识符使用PascalCase规则。
    privateStyle camelCase规则(第一个单词的首字母小写,其余单词的首字母大写)
    PublicStyle PascalCase规则(所有单词的首字母大写)
  • 尽量不要使用缩写。
    Message,而不要使用msg。
  • 不要使用匈牙利命名法。
public sealed class GrammarHelper
{   ...
		public QualifiedSymbol Optional(AnySymbol symbol)
		{ ... }
		private AnyMultiplicity optional =
		new OptionalMultiplicity();
}
关键字

C#中76个关键字:

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            int           interface 
internal   is        lock          long         namespace 
new         null      object       operator     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      
while

5个在某些情况下是关键字:
get set value add remove
C#中有76个在任何情况下都有固定意思的关键字。另外还有5个在特定情况下才有固定意思的标识符。例如,value能用来作为变量名,但有一种情况例外,那就是它用作属性/索引器的set语句的时候是一关键字。
但你可以在关键字前加@来使它可以用作变量名:
int @int = 42;
不过在一般情况下不要使用这种变量名。
你也可以使用@来产生跨越几行的字符串,这对于产生正则表达式非常有用。例如:

		string pattern = @"
		(               # start the group
		  abra(cad)?  # match abra and optional cad
		)+";           # one or more occurrences

如果你要在字符串中包含双引号,那你可以这样:
string quote = @"""quote""";

标点符号

{ 和 } 组成语句块
分号表示一个语句的结束

using System; 
public sealed class Hiker 
{ 
    public static void Main() 
    { 
        int result; 
        result = 9 * 6;
        int thirteen;
        thirteen = 13;
        Console.Write(result / thirteen);
        Console.Write(result % thirteen); 
    } 
} 

一个C#的“类/结构/枚举”的定义不需要一个终止的分号。

		public sealed class Hiker
		{
			...
		} // 没有;是正确的

然而你可以使用一个终止的分号,但对程序没有任何影响:

		public sealed class Hiker
		{
			...
		}; //有;是可以的但不推荐

在Java中,一个函数的定义中可以有一个结尾分号,但在C#中是不允许的。

		public sealed class Hiker
		{
			public void Hitch() { ... }; //;是不正确的
		} // 没有;是正确的
声明

声明是在一个块中引入变量

  • 每个变量有一个标识符和一个类型
  • 每个变量的类型不能被改变
using System; 
public sealed class Hiker 
{ 
    public static void Main() 
    { 
        int result; 
        result = 9 * 6; 
        int thirteen; 
        thirteen = 13; 
        Console.Write(result / thirteen); 
        Console.Write(result % thirteen); 
    } 
} 

这样声明一个变量是非法的:这个变量可能不会被用到。例如:

		if (...)
		         int x = 42; //编译时出错
			else
                 ...
表达式

表达式是用来计算的!

  • 每个表达式产生一个值
  • 每个表达式必须只有单边作用
  • 每个变量只有被赋值后才能使用
using System; 
public sealed class Hiker 
{ 
    public static void Main() 
    { 
        int result; 
        result = 9 * 6; 
        int thirteen; 
        thirteen = 13; 
        Console.Write(result / thirteen); 
        Console.Write(result % thirteen); 
    } 
} 

C#不允许任何一个表达式读取变量的值,除非编译器知道这个变量已经被初始化或已经被赋值。例如,下面的语句会导致编译器错误:

		int m;
		if (...) {
				m = 42;
		}
		Console.WriteLine(m);// 编译器错误,因为m有可能不会被赋值
取值
类型	           取值                                  解释
bool            true false                              布尔型
float              3.14                                  实型
double             3.141                               双精度型
char                'X'                                 字符型
int                  9                                    整型
string             "Hello"                               字符串
object              null                                  对象
操作符
操作符                                  类型
 + - * / %(取余数)                    算术
&& || ! ?:                             逻辑
< <= > >=                              关系
== !=                                  相等
=                                      赋值

9.编程风格

  • 较正规的编程风格
  • 在一个二元操作符的每一边都加一个空格
  • 在每一个逗号后面而不是前面加一个空格
  • 每一个关键字后面加一个空格
  • 一行一个语句
  • 分号前不要有空格
  • 函数的园括号和参数之间不加空格
  • 在一元操作符和操作数之间不加空格
  • 在一个二元操作符的每一边都加一个空格:
    Console.WriteLine("{0}", result / 13); //推荐
    Console.WriteLine("{0}", result/13); //不推荐
  • 在每一个逗号后面而不是前面加一个空格:
    Console.WriteLine("{0}", result / 13); //推荐
    Console.WriteLine("{0}",result / 13); //不推荐
  • 每一个关键字后面加一个空格:
    if (OneLine(comment)) … //推荐
    if(OneLine(comment)) … //不推荐
  • 分号前不要有空格:
    Console.WriteLine("{0}", result / 13); //推荐
    Console.WriteLine("{0}", result / 13) ; //不推荐
  • 函数的园括号和参数之间不加空格:
    if (OneLine(comment)) … //推荐
    if (OneLine( comment )) … //不推荐
  • 在一元操作符和操作数之间不加空格:
    ++keywordCount; //推荐
    ++ keywordCount; //不推荐
找错
bool checked; 
...                                         1

public static void main() 
{ ... }                                     2

int matched = symbol.Match(input) 
if (matched > 0) 
{ 
     ....   
}                                           3

char optional = "?"; 
string theory = 'complex';                  4 

int matched = 0_or_more(symbol);            5
...                                                                 

第1段程序的错误:checked是一个关键字
第2段程序的错误:不是main,而是Main
第3段程序的错误:变量声明语句没有分号
第4段程序的错误:字符值必须用单引号表示,字符串必须用双引号表示
第5段程序的错误:第一个错误是标识符不能以数字开头;第二个错误是不能用下划线作标识符。

猜你喜欢

转载自blog.csdn.net/le_17_4_6/article/details/86636826