<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

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 凹凸精品视频分类国产品免费| 亚洲a无码综合a国产av中文| 边摸边脱吃奶边高潮视频免费| 777成影片免费观看| 中文字幕亚洲不卡在线亚瑟| 亚洲国产一区二区三区在线观看| 国产成人精品免费视频大| 亚洲码国产精品高潮在线| 日韩精品无码永久免费网站| 国产又大又黑又粗免费视频 | 99久久国产免费中文无字幕| 亚洲综合色区在线观看| 在线亚洲v日韩v| 精品久久久久久久免费人妻| 亚洲熟妇AV一区二区三区浪潮| 最近中文字幕完整免费视频ww| 亚洲AV成人无码久久精品老人| 中文字幕的电影免费网站| 亚洲国产精品一区二区第一页免 | 中文字幕免费观看视频| 亚洲 国产 图片| 深夜福利在线免费观看| 免费看国产精品麻豆| 国产亚洲情侣久久精品| 亚洲精品无码日韩国产不卡?V| 全部在线播放免费毛片| 国产亚洲精品看片在线观看 | 性色av无码免费一区二区三区| 亚洲fuli在线观看| 无限动漫网在线观看免费| 亚洲国产视频久久| 亚洲色偷偷狠狠综合网| 久久久久久毛片免费播放| 亚洲中文字幕无码av| 亚洲综合熟女久久久30p| 亚洲成年人免费网站| 日本高清不卡中文字幕免费| 亚洲国产精品久久久天堂| 四虎影院在线免费播放| a级毛片视频免费观看| 亚洲字幕AV一区二区三区四区 |