以前是学C++的,初次学微软的C#头都大了。什么字段,常量,属性,方法......微软把别人的东西拿来糅合在C#里,弄成了一个“大杂烩”。其实,说到底,“字段”不就是“变量”吗,所谓的“方法”不就是“函数”吗,故弄玄虚!从MSDN上弄来了详细的介绍,看下面:
字段是其包含类型的“成员”。
从实例 A 所做的更改将立刻呈现在实例 B 和 C 上(如果它们访问该字段)。
存储由公共属性公开的数据的私有字段称为“后备存储”或“支持字段”。
不在单个方法范围外部使用的变量应在方法体自身范围内声明为局部变量。
例如:
1 public class CalendarEntry 2 { 3 // private field 4 private DateTime date; 5 6 // public field (Generally not recommended.) 7 public string day; 8 9 // Public property exposes date field safely. 10 public DateTime Date 11 { 12 get 13 { 14 return date; 15 } 16 set 17 { 18 // Set some reasonable boundaries for likely birth dates. 19 if (value.Year > 1900 && value.Year <= DateTime.Today.Year) 20 { 21 date = value; 22 } 23 else 24 throw new ArgumentOutOfRangeException(); 25 } 26 27 } 28 29 // Public method also exposes date field safely. 30 // Example call: birthday.SetDate("1975, 6, 30"); 31 public void SetDate(string dateString) 32 { 33 DateTime dt = Convert.ToDateTime(dateString); 34 35 // Set some reasonable boundaries for likely birth dates. 36 if (dt.Year > 1900 && dt.Year <= DateTime.Today.Year) 37 { 38 date = dt; 39 } 40 else 41 throw new ArgumentOutOfRangeException(); 42 } 43 44 public TimeSpan GetTimeSpan(string dateString) 45 { 46 DateTime dt = Convert.ToDateTime(dateString); 47 48 if (dt != null && dt.Ticks < date.Ticks) 49 { 50 return date - dt; 51 } 52 else 53 throw new ArgumentOutOfRangeException(); 54 55 } 56 }
例如:
1 CalendarEntry birthday = new CalendarEntry(); 2 birthday.day = "Saturday";
day,如下例所示:
public class CalendarDateWithInitialization { public string day = "Monday"; //... }
如果构造函数为字段赋值,则该值将覆盖字段声明期间给出的任何值。
访问修饰符(C# 编程指南)。
静态类和静态类成员(C# 编程指南)。
常量(C# 编程指南)。
常量:
readonly 修饰符创建在运行时初始化一次即不可再更改的类、结构或数组。
const 方法、属性或事件。
enum(C# 参考)。
例如:
1 class Calendar1 2 { 3 public const int months = 12; 4 }
const 字段不能通过引用传递,并且不能在表达式中作为左值出现。
可以同时声明多个相同类型的常量,例如:
class Calendar2 { const int months = 12, weeks = 52, days = 365; }
例如:
1 class Calendar3 2 { 3 const int months = 12; 4 const int weeks = 52; 5 const int days = 365; 6 7 const double daysPerWeek = (double) days / (double) weeks; 8 const double daysPerMonth = (double) days / (double) months; 9 }
访问修饰符(C# 编程指南)。
例如:
int birthstones = Calendar.months;
属性:
这使得可以轻松访问数据,此外还有助于提高方法的安全性和灵活性。
Hours 属性的访问器执行小时和秒之间的转换。
1 class TimePeriod 2 { 3 private double seconds; 4 5 public double Hours 6 { 7 get { return seconds / 3600; } 8 set { seconds = value * 3600; } 9 } 10 } 11 12 13 class Program 14 { 15 static void Main() 16 { 17 TimePeriod t = new TimePeriod(); 18 19 // Assigning the Hours property causes the 'set' accessor to be called. 20 t.Hours = 24; 21 22 // Evaluating the Hours property causes the 'get' accessor to be called. 23 System.Console.WriteLine("Time in hours: " + t.Hours); 24 } 25 } 26 // Output: Time in hours: 24
-
属性使类能够以一种公开的方法获取和设置值,同时隐藏实现或验证代码。
-
限制访问器可访问性(C# 编程指南)。
-
set 取值函数分配的值。
-
set 取值函数的属性是只读的。
-
自动实现的属性(C# 编程指南)。
方法:
Main 方法是每个 C# 应用程序的入口点,在启动程序时由公共语言运行时 (CLR) 调用。
这些部分统称为方法的“签名”。
下面的类包含三个方法:
1 abstract class Motorcycle 2 { 3 // Anyone can call this. 4 public void StartEngine() {/* Method statements here */ } 5 6 // Only derived classes can call this. 7 protected void AddGas(int gallons) { /* Method statements here */ } 8 9 // Derived classes can override the base class implementation. 10 public virtual int Drive(int miles, int speed) { /* Method statements here */ return 1; } 11 12 // Derived classes must implement this. 13 public abstract double GetTopSpeed(); 14 }
Motorcycle类的方法:
1 class TestMotorcycle : Motorcycle 2 { 3 4 public override double GetTopSpeed() 5 { 6 return 108.4; 7 } 8 9 static void Main() 10 { 11 12 TestMotorcycle moto = new TestMotorcycle(); 13 14 moto.StartEngine(); 15 moto.AddGas(15); 16 moto.Drive(5, 20); 17 double speed = moto.GetTopSpeed(); 18 Console.WriteLine("My top speed is {0}", speed); 19 } 20 }
例如:
1 public void Caller() 2 { 3 int numA = 4; 4 // Call with an int variable. 5 int productA = Square(numA); 6 7 int numB = 32; 8 // Call with another int variable. 9 int productB = Square(numB); 10 11 // Call with an integer literal. 12 int productC = Square(12); 13 14 // Call with an expression that evaulates to int. 15 productC = Square(productA * 3); 16 } 17 18 int Square(int i) 19 { 20 // Store input argument in a local variable. 21 int input = i; 22 return input * input; 23 }
值类型表(C# 参考)。
如果更改对象的成员通过使用该引用,更改反映在被调用的方法的参数,因此,即使通过对象的值。
class 关键字,则创建一个引用类型。
public class SampleRefType { public int value; }
ModifyObject。
1 public static void TestRefType() 2 { 3 SampleRefType rt = new SampleRefType(); 4 rt.value = 44; 5 ModifyObject(rt); 6 Console.WriteLine(rt.value); 7 } 8 static void ModifyObject(SampleRefType obj) 9 { 10 obj.value = 33; 11 }
TestRefType 方法显示 33 作为输出。