标签:back blog ges html 引用 size alt 大量 内容
一.const与readonly的争议
二.什么时候开始不可变?
1 public class MathHelper 2 { 3 public const float PI= 3.14159F; 4 }
1 static void Main(string[] args) 2 { 3 float pi= MathHelper.PI; 4 }
1 public class MathHelper 2 { 3 public readonly float PI; 4 }
1 public class MathHelper 2 { 3 public MathHelper() 4 { 5 this.PI = 3.15F; 6 this.PI = 3.14F; 7 } 8 public readonly float PI; 9 }
1 static void Main(string[] args) 2 { 3 MathHelper m = new MathHelper(); 4 Console.WriteLine(m.PI); 5 }
1 public class MathHelper 2 { 3 public MathHelper(float pi) 4 { 5 this.PI = pi; 6 } 7 public readonly float PI; 8 }
1 static void Main(string[] args) 2 { 3 MathHelper m1 = new MathHelper(3.14F); 4 Console.WriteLine(m1.PI); 5 6 MathHelper m2 = new MathHelper(3.15F); 7 Console.WriteLine(m2.PI); 8 9 Console.Read(); 10 }
1 public class MathHelper 2 { 3 public readonly float PI=3.15F; 4 }
1 public class MathHelper 2 { 3 public static readonly float PI=3.14F; 4 }
1 static void Main(string[] args) 2 { 3 Console.WriteLine(MathHelper.PI); 4 Console.Read(); 5 }
三.什么是不可变的?
1 public class Alphabet 2 { 3 public static readonly Char[] Letters = new Char[] {‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘ }; 4 }
1 static void Main(string[] args) 2 { 3 Alphabet.Letters[0] = ‘a‘; 4 Alphabet.Letters[0] = ‘b‘; 5 Alphabet.Letters[0] = ‘c‘; 6 Alphabet.Letters[0] = ‘d‘; 7 Alphabet.Letters[0] = ‘e‘; 8 Alphabet.Letters[0] = ‘f‘; 9 Console.WriteLine(Alphabet.Letters.Length); 10 Console.Read(); 11 }
四:总结
参考文档:
标签:back blog ges html 引用 size alt 大量 内容
原文地址:http://www.cnblogs.com/gudi/p/6027403.html