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

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

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

    JUST DO IT ~

    我只想當(dāng)個(gè)程序員

    c# readonly const 區(qū)別

     

    Const   靜態(tài)的常量。

    Readonly

    final java 一樣概念

    靜態(tài)的常量。


    常量定義:在編譯時(shí)。運(yùn)行時(shí)不可以修改。
    必須定義成:成員變量。




    常量必須是
    integral 完整類型type (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, or string),an enumeration, or a reference to null.



    其實(shí)就是基本類型 ==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 靜態(tài)變量一樣用。不需要static 關(guān)鍵字。

    Constants are accessed as if they were static fields, although they cannot use the static keyword.

    常量可以標(biāo)記的前綴:
    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.

    動靜態(tài) 都可以。

    Class Instance 變量。的屬性是可以修改的

    Struct 的不是不可以 修改他的屬性的。

    readonly字段的賦值只能作為字段聲明的組成部分出現(xiàn),或在同一類中的實(shí)例構(gòu)造函數(shù)靜態(tài)構(gòu)造函數(shù)中出現(xiàn)。


    一個(gè)只讀成員,表現(xiàn)出 不可以被修改。
    只讀成員, 初始化 在運(yùn)行時(shí)。。。

    可以初始化 在  定義 或者 構(gòu)造




    public class MyClass
                {
                public readonly double PI = 3.14159;
                }

    or

    public class MyClass
                {
                public readonly double PI;
                 
                public MyClass()
                {
                PI = 3.14159;
                }
                }

    注意
     只讀成員  不是 隱式的靜態(tài),但是可以用static 修飾。

    readonly 關(guān)鍵字 可以用 復(fù)雜類型,可以用new 關(guān)鍵子 初始化。


    readonly 不能是 enu 枚舉類型。

        const string sv = "abc" ;

        const float pii = 3.1415926f;

        const static string psss = "aaa"// 默認(rèn)就是的static 并且這樣不行

    const string sss = null;



    readonly string rdstr = System.Windows.Forms.Application.StartupPath + "aaa";

       Test() { // 構(gòu)造函數(shù)。

        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 對引用類型(字符串除外)的常量進(jìn)行初始化


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

            /*

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

            非靜態(tài)的字段、方法或?qū)傩?#8220;Acme.Collections.Test.rdstr”要求對象引用

             */

            Console.WriteLine(path);



    static 變量

    static 關(guān)鍵字修飾。
    被訪問 不是在 實(shí)例創(chuàng)建時(shí)候。
    靜態(tài)方法和屬性訪問 靜態(tài)事件。

     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.














    *
    需要注意的一個(gè)問題是:

    對于一個(gè) readonlyReference類型,只是被限定不能進(jìn)行賦值(寫)操作而已。而對其成員的讀寫仍然是不受限制的。

    public static readonly Class1 my = new Class1();

    my.SomeProperty = 10;//
    正常
    my = new Class1(); //出錯(cuò),該對象是只讀的

    但是,如果上例中的 Class1不是一個(gè) Class而是一個(gè) struct,那么后面的兩個(gè)語句就都會出錯(cuò)。

    static readonly:

    Java 中 static是當(dāng)載入一個(gè)類時(shí)執(zhí)行一次的。

    C#中是怎么執(zhí)行的,我沒有查到。很奇怪幾乎每本java的書都會說static的問題,C#的往往只說怎么用,但是應(yīng)該是在main函數(shù)調(diào)用之前初始化,所以static readonly也是運(yùn)行時(shí)的,可以用變量付值,如:

    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

    導(dǎo)航

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

    統(tǒng)計(jì)

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

    常用鏈接

    留言簿(3)

    隨筆分類(352)

    收藏夾(19)

    關(guān)注的blog

    手冊

    搜索

    •  

    積分與排名

    • 積分 - 301034
    • 排名 - 193

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产日韩在线人成下载| 99热这里只有精品6免费| 在线永久免费的视频草莓| 日本高清免费中文在线看| 亚洲五月综合缴情婷婷| 亚洲国产精品自在线一区二区 | 中文字幕亚洲专区| 在线观看人成视频免费| 91嫩草免费国产永久入口| 野花香在线视频免费观看大全| 麻豆亚洲AV永久无码精品久久| 野花高清在线观看免费完整版中文| 亚洲AV日韩AV无码污污网站| 亚洲国产精品成人AV无码久久综合影院| 中文字幕手机在线免费看电影| 噜噜噜亚洲色成人网站∨ | 久热综合在线亚洲精品| 免费无码A片一区二三区| 久久精品人成免费| 国内少妇偷人精品视频免费| 久久久WWW免费人成精品| 日本高清不卡中文字幕免费| 免费人成在线观看播放a| 亚洲成a∧人片在线观看无码| 亚洲精品无码专区在线在线播放| 免费国产成人高清在线观看网站 | 亚洲成av人片不卡无码久久| 日韩人妻无码精品久久免费一| 亚洲精品人成网线在线播放va| 亚洲国产精品无码久久久秋霞2 | 亚洲精品视频在线观看你懂的| 131美女爱做免费毛片| 日韩免费无码视频一区二区三区 | 91在线视频免费看| 精品国产sm捆绑最大网免费站| 一边摸一边桶一边脱免费视频| 亚洲国产最大av| 久久亚洲精品国产亚洲老地址 | 午夜视频在线免费观看| 337P日本欧洲亚洲大胆艺术图| 亚洲福利电影一区二区?|