<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

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲?v无码国产在丝袜线观看| 国产成人无码免费看视频软件 | 亚洲精华国产精华精华液网站| 久久一区二区三区免费播放| 亚洲另类激情综合偷自拍图| 久久久WWW成人免费精品| 亚洲综合色区在线观看| 一级毛片在线播放免费| 中文字幕中韩乱码亚洲大片| 高清永久免费观看| 精品亚洲永久免费精品| 污污网站免费观看| 亚洲另类小说图片| 国产精品成人四虎免费视频| 七次郎成人免费线路视频| 狠狠综合久久综合88亚洲| 精品视频一区二区三区免费| 亚洲综合精品一二三区在线| 2021国产精品成人免费视频| 亚洲一级毛片免费观看| 尤物永久免费AV无码网站| 九九久久精品国产免费看小说| 亚洲无码在线播放| 91精品免费观看| 亚洲色大成网站www久久九| 又粗又大又长又爽免费视频| 两性色午夜视频免费播放| 久久精品国产亚洲77777| 美女裸身网站免费看免费网站| 亚洲午夜成人精品无码色欲| 免费一看一级毛片全播放| 成人爽a毛片免费| 亚洲综合丁香婷婷六月香| 亚洲 小说区 图片区 都市| 免费一级毛片无毒不卡| 亚洲香蕉久久一区二区三区四区| 四虎影视在线永久免费看黄| 久久国产精品2020免费m3u8| 蜜臀AV免费一区二区三区| 精品亚洲国产成人av| 亚洲AV无码成人精品区天堂 |