c# readonly const 区别
Const 静态的常量。 |
Readonly |
和final java 一样概念 静态的常量。 |
动静态 都可以。 Class Instance 变量。的属性是可以修改的 Struct 的不是不可以 修改他的属性的。 readonly字段的赋值只能作为字段声明的组成部分出现,或在同一类中的实例构造函数或静态构造函数中出现。 public class MyClass { public readonly double PI = 3.14159; } or public class MyClass { public readonly double PI; public MyClass() { PI = 3.14159; } } 注意 |
const string sv = "abc" ; const float pii = 3.1415926f; const static string psss = "aaa"; // 默认就是的static 并且这样不行 |
readonly string rdstr = System.Windows.Forms.Application.StartupPath + "aaa"; Test() { // 构造函数。 rdstr = "s" + sv; } private static readonly string path = System.Windows.Forms.Application.StartupPath + "aaa"; |
想赋值都不行。 只能用null const Test testt = new Test(); |
Console.WriteLine( new Test().rdstr); /* Test.cs(142,27): error CS0120: 非静态的字段、方法或属性“Acme.Collections.Test.rdstr”要求对象引用 */ Console.WriteLine(path); |
static 变量 The |
* 需要注意的一个问题是:
对于一个 readonly的 Reference类型,只是被限定不能进行赋值(写)操作而已。而对其成员的读写仍然是不受限制的。
public static readonly Class1 my = new Class1();
…
my.SomeProperty = 10;//正常
my = new Class1(); //出错,该对象是只读的
但是,如果上例中的 Class1不是一个 Class而是一个 struct,那么后面的两个语句就都会出错。
static readonly:
Java 中 static是当载入一个类时执行一次的。
C#中是怎么执行的,我没有查到。很奇怪几乎每本java的书都会说static的问题,C#的往往只说怎么用,但是应该是在main函数调用之前初始化,所以static readonly也是运行时的,可以用变量付值,如:
private static readonly string path = System.Windows.Forms.Application.StartupPath + “aaa”;
引用下文
http://dev.csdn.net/develop/article/82/82998.shtm
http://en.csharp-online.net/const,_static_and_readonly
const, static and readonly
From C# Online.NET (CSharp-Online.NET)—your free C# and .NET encyclopedia
Exam Prep. Guides |
Exam 70-536 Study Guide |
1. Types and collections
2. Process, threading,… |
edit |
Contents[hide]
|
Within a class, const
, static
and readonly
members are special in comparison to the other modifiers.
const
vs. readonly
const
and readonly
perform a similar function on data members, but they have a few important differences.
const
A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const
keyword and must be initialized as they are declared. For example;
public class MyClass { public const double PI = 3.14159; }
PI
cannot be changed in the application anywhere else in the code as this will cause a compiler error.
Constants must be of an integral type (sbyte
, byte
, short
, ushort
, int
, uint
, long
, ulong
, char
, float
, double
, decimal
, bool
, or string
), an enumeration, or a reference to null
.
Since classes or structures are initialized at run time with the new
keyword, and not at compile time, you can't set a constant to a class or structure.
Constants can be marked as public
, private
, protected
, internal
, or protected internal
.
Constants are accessed as if they were static fields, although they cannot use the static
keyword.
To use a constant outside of the class that it is declared in, you must fully qualify it using the class name.
readonly
A read only member is like a constant in that it represents an unchanging value. The difference is that a readonly
member can be initialized at runtime, in a constructor as well being able to be initialized as they are declared. For example:
public class MyClass { public readonly double PI = 3.14159; }
or
public class MyClass { public readonly double PI; public MyClass() { PI = 3.14159; } }
Because a readonly
field can be initialized either at the declaration or in a constructor, readonly
fields can have different values depending on the constructor used. A readonly
field can also be used for runtime constants as in the following example:
public static readonly uint l1 = (uint)DateTime.Now.Ticks;
Notes
readonly
members are not implicitlystatic
, and therefore thestatic
keyword can be applied to areadonly
field explicitly if required.
- A
readonly
member can hold a complex object by using thenew
keyword at initialization.
readonly
members cannot hold enumerations.
static
Use of the static
modifier to declare a static
member, means that the member is no longer tied to a specific object. This means that the member can be accessed without creating an instance of the class. Only one copy of static
fields and events exists, and static
methods and properties can only access static
fields and static
events. For example:
public class Car { public static int NumberOfWheels = 4; }
The static
modifier can be used with classes, fields, methods, properties, operators, events and constructors, but cannot be used with indexers, destructors, or types other than classes.
static
members are initialized before the static
member is accessed for the first time, and before the static
constructor, if any is called. To access a static
class member, use the name of the class instead of a variable name to specify the location of the member. For example:
int i = Car.NumberOfWheels;
MSDN references
- Constants (C# Programming Guide)
const
(C# Reference)
readonly
(C# Reference)
static
(C# Reference)- Static Classes and Static Class Members (C# Programming Guide)
Categories: Exam 70-536 Study Guide | Exam 70-536 Study Guide: Skill Set 1