>  기사  >  백엔드 개발  >  C# 읽기 전용

C# 읽기 전용

王林
王林원래의
2024-09-03 15:31:31170검색

읽기 전용 필드는 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이라는 클래스가 정의됩니다. 그런 다음 읽기 전용 필드에 저장된 값을 읽고 화면에 출력으로 인쇄하는 클래스 검사 인스턴스가 정의되는 기본 메서드가 호출됩니다. 출력은 위의 스냅샷에 표시됩니다.

예시 #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이라는 네임스페이스가 정의되어 있습니다. 그런 다음 double 값을 저장하기 위해 읽기 전용 필드가 정의된 check라는 클래스가 정의됩니다. 그런 다음 기본 메서드가 호출되는 example이라는 클래스가 정의됩니다. 그런 다음 읽기 전용 필드에 저장된 값을 읽고 화면에 출력으로 인쇄하는 클래스 검사 인스턴스가 정의되는 기본 메서드가 호출됩니다. 출력은 위의 스냅샷에 표시됩니다.

예시 #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이라는 클래스가 정의됩니다. 그런 다음 읽기 전용 필드에 저장된 값을 읽고 화면에 출력으로 인쇄하는 클래스 검사 인스턴스가 정의되는 기본 메서드가 호출됩니다. 출력은 위의 스냅샷에 표시됩니다.

결론

이 튜토리얼에서는 정의를 통해 C#의 읽기 전용 키워드 개념, 읽기 전용 구문, 프로그래밍 예제 및 해당 출력을 통해 C#의 읽기 전용 작동 방식을 이해합니다.

위 내용은 C# 읽기 전용의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:C#의 부분적다음 기사:C#의 부분적