C Sharp/Syntax
< C Sharp
C#的面向对象本质,使C#程序高层结构用类来定义。
语居
编辑int sampleVariable; // declaring a variable
sampleVariable = 5; // assigning a value
Method(); // calling an instance method
SampleClass sampleObject = new SampleClass(); // creating a new instance of a class
sampleObject.ObjectMethod(); // calling a member function of an object
// executing a "for" loop with an embedded "if" statement
for (int i = 0; i < upperLimit; i++)
{
if (SampleClass.SampleStaticMethodReturningBoolean(i))
{
sum += sampleObject.SampleMethodReturningInteger(i);
}
}
语句块
编辑用花括号围起来的多个语句形成一个语句块。
private void MyMethod(int integerValue)
{ // This block of code is the body of "MyMethod()"
// The 'integerValue' integer parameter is accessible to everything in the method
int methodLevelVariable; // This variable is accessible to everything in the method
if (integerValue == 2)
{
// methodLevelVariable is still accessible here
int limitedVariable; // This variable is only accessible to code in the, if block
DoSomeWork(limitedVariable);
}
// limitedVariable is no longer accessible here
} // Here ends the code block for the body of "MyMethod()".
大小写敏感
编辑C#是大小写敏感语言。
下述两个变量myInteger
和MyInteger
是不同的:
int myInteger = 3;
int MyInteger = 5;
C#有个预定义的Console
处理命令行操作。
// Compiler error!
console.writeline("Hello");
正确写法:
Console.WriteLine("Hello");