c# readonly const 区别

c# readonly const 区别

 

Const   静态的常量。

Readonly

final java 一样概念

静态的常量。 常量定义:在编译时。运行时不可以修改。 必须定义成:成员变量。 常量必须是 integral 完整类型type (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, or string),an enumeration, or a reference to null. 其实就是基本类型 ==java  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.     常量 定义后就和 static 静态变量一样用。不需要static 关键字。 Constants are accessed as if they were static fields, although they cannot use the static keyword. 常量可以标记的前缀: public, private, protected, internal, or protected internal. Constants can be marked as public, private, protected, internal, or protected internal. 类名.常量 To use a constant outside of the class that it is declared in, you must fully qualify it using the class name.

动静态 都可以。

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;
            }
            }

注意  只读成员  不是 隐式的静态,但是可以用static 修饰。 readonly 关键字 可以用 复杂类型,可以用new 关键子 初始化。 readonly 不能是 enu 枚举类型。

    const string sv = "abc" ;

    const float pii = 3.1415926f;

    const static string psss = "aaa"// 默认就是的static 并且这样不行 const string sss = null;

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(); Test.cs(122,24): error CS0134:         “Acme.Collections.Test.testt”的类型为“Acme.Collections.Test”。只能用         null 对引用类型(字符串除外)的常量进行初始化

        Console.WriteLine( new Test().rdstr);  

        /*

         Test.cs(142,27): error CS0120:

        非静态的字段、方法或属性“Acme.Collections.Test.rdstr”要求对象引用

         */

        Console.WriteLine(path);

static 变量 static 关键字修饰。 被访问 不是在 实例创建时候。 静态方法和属性访问 静态事件。  means that the member is no longer tied to a specific object.?

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.

*
需要注意的一个问题是:

对于一个 readonlyReference类型,只是被限定不能进行赋值(写)操作而已。而对其成员的读写仍然是不受限制的。 public static readonly Class1 my = new Class1(); … my.SomeProperty = 10;//正常 my = new Class1(); //出错,该对象是只读的 但是,如果上例中的 Class1不是一个 Class而是一个 struct,那么后面的两个语句就都会出错。 static readonlystatic是当载入一个类时执行一次的。 C#中是怎么执行的,我没有查到。很奇怪几乎每本java的书都会说static的问题,C#的往往只说怎么用,但是应该是在main函数调用之前初始化,所以static readonly也是运行时的,可以用变量付值,如: private static readonly string path = System.Windows.Forms.Application.StartupPath + “aaa”; 引用下文: Java

http://www.blogjava.net/gddg/archive/2008/01/27/178070.html http://dev.csdn.net/develop/article/82/82998.shtm

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值