>  기사  >  백엔드 개발  >  C# 인턴()

C# 인턴()

PHPz
PHPz원래의
2024-09-03 15:17:30410검색

C#의 intern() 함수는 메모리에서 특정 문자열의 참조를 찾는 데 사용됩니다. 이 메서드는 메모리 영역을 검색하여 일치하는 문자열을 찾는 동안 제공된 문자열과 일치하는 문자열에 대한 참조를 찾습니다. 일치하는 항목이 발견되면 해당 문자열에 대한 참조가 다시 제공됩니다.

구문

public static string Intern(String string)

어디,

string은 메모리 영역에서 참조를 검색해야 하는 문자열입니다.

C#에서 intern()은 어떻게 작동하나요?

  • 메모리 영역에서 주어진 문자열과 일치하는 문자열의 참조를 검색해야 할 때마다 C#에서는 Intern() 메서드를 사용합니다.
  • 메서드가 호출될 때마다 지정된 문자열과 일치하는 메모리 영역의 문자열에 대한 참조를 검색합니다. 지정된 문자열과 메모리 영역의 문자열이 일치하면 메서드는 참조를 반환합니다.
  • 이 메소드는 주어진 문자열을 메모리 영역에 추가하고, 주어진 문자열이 일치하고 메모리 영역에 아직 존재하지 않는 경우 참조를 반환합니다.

C# intern() 구현 예

다음은 언급된 예시입니다.

예시 #1

Intern() 메서드를 시연하고 문자열에 대한 참조가 ReferenceEquals 메서드를 사용하여 동일한지 여부를 확인하는 C# 프로그램:

코드:

using System;
//a class called program is defined
public class Program
{
//main method is called
public static void Main(string[] args)
{
//a string variable is used to store the first string
string str1 = "Welcome to C#";
//another string variable is used to store the reference of the string one using intern method
string str2 = string.Intern(str1);
Console.WriteLine("The value of the string one is: {0}",str1);
Console.WriteLine("The value of the string two after using intern method on string one is: {0}",str2);
//ReferenceEquals method is used to check if the two strings are pointing to the same reference in the memory area or not
Console.WriteLine("If the references of the two objects are equal: {0}", Object.ReferenceEquals(str1, str2));
}
}

출력:

C# 인턴()

설명: 위 프로그램은 Program이라는 클래스를 정의합니다. 프로그램은 두 개의 문자열 변수를 지정하는 기본 프로시저를 호출합니다. intern() 메서드는 새 참조를 생성하고 첫 번째 문자열의 참조가 메모리 공간에 아직 존재하지 않는 경우 이를 반환합니다. 그런 다음 프로그램은 Object.ReferenceEquals 메서드를 활용하여 주어진 두 문자열의 참조가 일치하는지 여부를 확인합니다.

예시 #2

Intern() 메서드를 시연하고 문자열에 대한 참조가 ReferenceEquals 메서드를 사용하여 동일한지 여부를 확인하는 C# 프로그램:

코드:

using System;
//a class called program is defined
public class Program
{
//main method is called
public static void Main(string[] args)
{
//a string variable is used to store the first string
string str1 = "Welcome to";
string str2 = "Welcome to C#";
//another string variable is used to store the reference of the string one using intern method
string str3 = string.Intern(str1 + " C#");
Console.WriteLine("The value of the string one is: {0}",str1);
Console.WriteLine("The value of the string two is: {0}",str2);
Console.WriteLine("The value of the string three after using intern method on string one is: {0}",str3);
//ReferenceEquals method is used to check if the two strings are pointing to the same reference in the memory area or not
Console.WriteLine("If the references of the two objects are equal: {0}", Object.ReferenceEquals(str2, str3));
}
}

출력:

C# 인턴()

설명: 위 프로그램은 Program이라는 클래스를 정의합니다. str2로 표시되는 하나의 변수는 메모리 영역에서 참조를 검색해야 하는 문자열을 저장합니다. 문자열 str3은 문자열 str1과 str3의 조합입니다. 따라서 두 문자열 모두 동일한 참조를 반환하더라도 문자열 str2의 참조는 str3의 참조와 일치하지 않습니다. 메모리 영역에 문자열에 대한 참조가 없으면 Intern() 메서드는 새 참조를 만들어 반환합니다. 결과적으로 문자열 str3은 문자열 str1과 자신을 결합합니다. 그런 다음 Object.ReferenceEquals 메서드는 두 문자열의 참조가 일치하는지 확인하고 문자열 str2의 참조가 문자열 str3의 참조와 일치하지 않으므로 false를 반환합니다.

결론

이 튜토리얼에서는 프로그래밍 예제와 출력을 통해 Intern() 메서드의 정의, 구문, 작동을 통해 C#의 Intern() 메서드 개념을 이해합니다.

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

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