ホームページ  >  記事  >  バックエンド開発  >  C# 読み取り専用

C# 読み取り専用

王林
王林オリジナル
2024-09-03 15:31:31171ブラウズ

読み取り専用フィールドは、C# でキーワード read-only を使用することでアプリケーションで定義でき、読み取り専用フィールドの値の初期化は宣言中またはコンストラクターで行うことができます。キーワード read-only を使用して定義された読み取り専用フィールドの評価は実行時に行われ、この読み取り専用キーワードは、フィールドが読み取り専用として定義されている場合は常に、文字列、数値、NULL 参照、またはブール値とともに使用できます。ただし、フィールドが定義されているコンストラクターの実行が終了した場合、フィールドの値は変更できません。値がいつでも変更される可能性があるフィールドで読み取り専用キーワードを使用することはお勧めできません。このトピックでは、C# 読み取り専用について学習します。

構文:

readonly data_type field_name = "value";

ここで、data_type は読み取り専用フィールドのデータ型です。

field_name はフィールドの名前です。

C# での読み取り専用の仕組み

以下は作業内容です:

  • 読み取り専用のフィールドを定義する必要がある場合。 C# では読み取り専用キーワードを使用します。
  • 読み取り専用フィールドの値の初期化は、宣言中またはコンストラクター内で実行できます。
  • キーワード read-only を使用して定義された読み取り専用フィールドの評価は、実行時に行われます。
  • 読み取り専用キーワードは、文字列、数値、NULL 参照、またはブール値とともに使用できます。
  • フィールドが読み取り専用として定義されている場合、そのフィールドが定義されているコンストラクターの実行が終了すると、フィールドの値は変更できません。
  • 値がいつでも変更される可能性があるフィールドで読み取り専用キーワードを使用することはお勧めできません。

C# 読み取り専用の例

ここでは、以下に挙げる例について説明します。

例 #1

読み取り専用フィールドに格納されている値を読み取るための読み取り専用フィールドを示す C# プログラム。

コード:

using System.IO;
using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined within which the read only field is defined to store the string
class check
{
public readonly string stringname = "Welcome to C Sharp";
}
//a class called example is defined within which the main method is called
class example
{
//main method is called within which the instance of the class check is defined to which reads the value stored in the read only field and prints as output on the screen
static void Main(string[] args)
{
check checkvar = new check();
Console.WriteLine(checkvar.stringname);
Console.ReadLine();
}
}
}

出力:

C# 読み取り専用

上記のプログラムでは、programという名前空間が定義されています。次に、check というクラスが定義され、その中に文字列を格納するための読み取り専用フィールドが定義されます。次に、example というクラスが定義され、その中で main メソッドが呼び出されます。次に、メイン メソッドが呼び出され、その中でクラス チェックのインスタンスが定義され、読み取り専用フィールドに格納されている値が読み取られ、出力として画面に表示されます。出力は上のスナップショットに示されています。

例 #2

読み取り専用フィールドに格納されている値を読み取るための読み取り専用フィールドを示す C# プログラム。

コード:

using System.IO;
using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined within which the read only field is defined to store the double value
class check
{
public readonly double num = 10.50;
}
//a class called example is defined within which the main method is called
class example
{
//main method is called within which the instance of the class check is defined to which reads the value stored in the read only field and prints as output on the screen
static void Main(string[] args)
{
check checkvar = new check();
Console.WriteLine("The value of the variable is: {0}",checkvar.num);
Console.ReadLine();
}
}
}

出力:

C# 読み取り専用

上記のプログラムでは、programという名前空間が定義されています。次に、check というクラスが定義され、その中に double 値を格納するための読み取り専用フィールドが定義されます。次に、example というクラスが定義され、その中で main メソッドが呼び出されます。次に、クラス チェックのインスタンスが定義されている main メソッドが呼び出され、読み取り専用フィールドに格納されている値が読み取られ、出力として画面に表示されます。出力は上のスナップショットに示されています。

例 #3

読み取り専用フィールドに格納されている値を読み取るための読み取り専用フィールドをデモする C# プログラム。

コード:

using System.IO;
using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined within which the read only field is defined to store the double value
class check
{
public readonly string authorname = "Shobha Shivakumar";
public readonly string bookname = "Meaning of life";
public readonly int publishingyear = 2020;
}
//a class called example is defined within which the main method is called
class example
{
//main method is called within which the instance of the class check is defined to which reads the value stored in the read only field and prints as output on the screen
static void Main(string[] args)
{
check checkvar = new check();
Console.WriteLine("The name of the author is: {0}",checkvar.authorname);
Console.WriteLine("The name of the book is: {0}",checkvar.bookname);
Console.WriteLine("The publishing year of the book is: {0}",checkvar.publishingyear);
Console.ReadLine();
}
}
}

出力:

C# 読み取り専用

上記のプログラムでは、programという名前空間が定義されています。次に、check というクラスが定義され、その中に文字列と整数値を格納するための読み取り専用フィールドが定義されます。次に、example というクラスが定義され、その中で main メソッドが呼び出されます。次に、メイン メソッドが呼び出され、その中でクラス チェックのインスタンスが定義され、読み取り専用フィールドに格納されている値が読み取られ、出力として画面に表示されます。出力は上のスナップショットに示されています。

結論

このチュートリアルでは、定義を通じて C# の読み取り専用キーワードの概念、読み取り専用の構文、プログラミング例とその出力を通じて C# での読み取り専用の仕組みを理解します。

以上がC# 読み取り専用の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:C# の部分的次の記事:C# の部分的