<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JUST DO IT ~

    我只想當個程序員

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


    Jump to: navigation, search
    Exam Prep. Guides
    Exam 70-536 Study Guide

    1. Types and collections

    2. Process, threading,…
    3. Embedding features
    4. Serialization, I/O
    5. .NET Security
    6. Interop., reflection,…
    7. Global., drawing, text

    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 implicitly static, and therefore the static keyword can be applied to a readonly field explicitly if required.
    • A readonly member can hold a complex object by using the new 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


    posted on 2008-01-27 21:26 小高 閱讀(3215) 評論(0)  編輯  收藏 所屬分類: DotNet

    導航

    <2008年1月>
    303112345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    統計

    • 隨筆 - 341
    • 文章 - 0
    • 評論 - 50
    • 引用 - 0

    常用鏈接

    留言簿(3)

    隨筆分類(352)

    收藏夾(19)

    關注的blog

    手冊

    搜索

    •  

    積分與排名

    • 積分 - 301202
    • 排名 - 193

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 两个人看的www免费视频| 搜日本一区二区三区免费高清视频| 成人性生交大片免费看好| 久久亚洲AV成人无码国产电影| 91精品免费在线观看| 亚洲欧洲日产国码二区首页| 亚洲香蕉免费有线视频| 一本色道久久88亚洲综合| 亚洲男人的天堂在线播放| 亚洲欧洲免费视频| 2048亚洲精品国产| 国产精品亚洲片夜色在线| 黄色片在线免费观看| 亚洲欧美日韩国产精品一区| 日本免费电影一区| 一出一进一爽一粗一大视频免费的| 亚洲女同成人AⅤ人片在线观看 | 国产精品成人69XXX免费视频| 国产亚洲色视频在线| 国产激情免费视频在线观看| 亚洲理论精品午夜电影| 黄色视频在线免费观看| 国产成人无码a区在线观看视频免费| 亚洲av无码成h人动漫无遮挡 | 女人张开腿给人桶免费视频| 亚洲爆乳无码专区www| 久久久久亚洲AV成人网人人网站| 嫩草成人永久免费观看| 亚洲欧美日韩中文高清www777| 亚洲精品国产自在久久 | 国产综合成人亚洲区| 亚洲热线99精品视频| 最近中文字幕高清免费中文字幕mv | 亚洲免费视频网址| 免费看国产精品麻豆| 亚洲一日韩欧美中文字幕在线| 亚洲一区二区三区免费在线观看 | 菠萝菠萝蜜在线免费视频| 成年女人18级毛片毛片免费 | 亚洲精品视频观看| 免费观看国产小粉嫩喷水|