首页 > 其他 > 详细

【转载】Declaring and Using a readonly Field

时间:2014-03-12 02:48:08      阅读:313      评论:0      收藏:0      [点我收藏+]

You can make a field in a class read-only by using the readonly modifier when the field is declared.

In the example below, code that creates or uses instances of the Dog class will be able to read the SerialNumber field, but not write to it.

bubuko.com,布布扣
 1 public class Dog
 2 {
 3     public string Name;
 4     public int Age;
 5     public readonly string SerialNumber;
 6 
 7     public Dog(string name, int age, string serNumber)
 8     {
 9         Name = name;
10         Age = age;
11         SerialNumber = serNumber;
12     }
13 }    
bubuko.com,布布扣

You can assign a value to a readonly field in only two different places:

  • In the class constructor
  • As part of the field‘s declartion
1 public readonly string BestFriend = "Man";

【转载】Declaring and Using a readonly Field,布布扣,bubuko.com

【转载】Declaring and Using a readonly Field

原文:http://www.cnblogs.com/yuthreestone/p/3594721.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!