基本語句
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 |