C#のstatic、readonlyとconstの比較
C# , readonly( ) const( ), 。
readonly , , , 。
const , , 。
:
public static readonly int A = 2; //A
public const int B = 3; //B
:
int C = A + B;
:
int C = A + 3;
, const B 3, readonly A 。
readonly , , , 。
const , , ( static , ), 。
const , 。const ( 、 )、 。 :
public const DateTime D = DateTime.MinValue;
readonly :
public readonly DateTime D = DateTime.MinValue;
readonly , , 。
const , :
public class Class1
{
public static readonly int A = 2; //A
public const int B = 3; //B
}
public class Class2
{
public static int C = Class1.A + Class1.B; // C A、B
}
Console.WriteLine(Class2.C); // "5"
Class1 Class2 , Class1 :
public class Class1
{
public static readonly int A = 4; //A
public const int B = 5; //B
}
Class1 ( : Class2), C :
Console.WriteLine(Class2.C); // "7"
, C :
public static int C = Class1.A + Class1.B;
:
public static int C = Class1.A + 3;
B , 。 Class2 , const 。
const , readonly, , 。
:
a. ( 、 、 )
b.
const , readonly 。
C#のstaticとJavaのstatic
簡単で、両者の使い方は完全に一致している.2つの側面から議論します.
1.変数はクラスに属し、インスタンスレベルではありません.クラス名のみで呼び出すことができ、インスタンスで呼び出すことはできません.
2.定義時に付与された場合、クラスの初期化時に、すべての静的変数の付与が最初に完了します.しかし、すべての静的変数の初期化順序は決定できないことに注意してください.
C#のconstとJavaのfinnal
長い間、私は両者が同じ役割を果たしていると思っていましたが、変数の初期化後に変更できないだけでなく、定義時または構造関数にのみ値を割り当てることができます.しかし、これは一面的なもので、以下で詳しく分析します.
1.修飾変数
正確にはC#のconstはJavaのstatic finalに等価であり、つまりJavaのfinalはstaticの機能を持たない.C#のconstにはstaticの機能があります.したがってC#ではpublic static const stringなどがpublic const stringになります.
2.修飾クラスとメソッド
このときJavaのfinalはC#のsealedに似ています.つまり、final修飾のクラスは継承されず、final修飾の方法は上書きされません.
C#のconstはクラスやメソッドを修飾できません.
質問:
1.プライベート静的メンバーの役割(private static変数)
字面は私有を表し、類外では使用できない.静的、グローバル変数.矛盾しているように見えるし、クラス外で使われてはいけないし、全体的に何の役にも立たない.クラスのグローバル化も意味がありますprivate static int a=5など変数aがクラスの初期化過程で優先的に初期化されることを保証することができる(コンストラクション関数が実行される前に).これにより、オブジェクトAの初期化にオブジェクトBのインスタンスが必要である場合、クラスAがコンストラクション関数でクラスBのインスタンスを使用できることを保証するために、このような説明を用いることができる.privateはまた、クラスBのインスタンスがクラスAでのみ使用され、良好なシール作用を果たすことを保証することができる.
2.プライベートエンドメンバーの役割(private final変数)
クラスコンストラクション関数が完了する前に、定義が定義されると変更されないメンバーの初期化を完了する必要があります.このメンバーは、このクラスでのみ使用できます.インスタンスは、サブクラスでは使用できません.
private static final修飾メンバーは、宣言時に付与され、構造関数で使用できることを保証します.private static final修飾メンバーは、通常、他のコンポーネントのインスタンスを表し、変数はクラス内のグローバル変数です.
private final修飾メンバーは、クラスのグローバルなプライベートメンバー変数であることを示し、クラスの初期化を完了するには、クラスの初期値を入力する必要があります.
C#constとstatic readonlyの違い
const:const修飾子で宣言されるメンバーを定数と呼び、コンパイル中にクライアントプログラムstatic readonlyに初期化して埋め込む:static readonly修飾子で宣言されるメンバーは依然として変数であり、定数と類似した使用方法があるにすぎない:クラスによるアクセス、初期化後は変更できない.しかし、定数とは異なり、この変数は実行期間に初期化されます.
C#constとstatic readonlyの違いの例:
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace Example02Lib
- {
- public class Class1
- {
- public const String strConst = "Const";
- public static readonly String strStaticReadonly = "StaticReadonly";
- //public const String strConst = "Const Changed";
- //public static readonly String strStaticReadonly = "StaticReadonly Changed";
- }
- }
クライアントコード:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Example02Lib;
- namespace Example02
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Example02 Class1 strConst , Example02Lib
- // Example02Lib.dll Example02.exe , Example02.exe
- // IDE !!
- // strConst , strStaticReadonly
- // Const , StaticReadonly
- Console.WriteLine("strConst : {0}", Class1.strConst);
- Console.WriteLine("strStaticReadonly : {0}", Class1.strStaticReadonly);
- Console.ReadLine();
- }
- }
- }
変更後の例:
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace Example02Lib
- {
- public class Class1
- {
- //public const String strConst = "Const";
- //public static readonly String strStaticReadonly = "StaticReadonly";
- public const String strConst = "Const Changed";
- public static readonly String strStaticReadonly = "StaticReadonly Changed";
- }
- }