C# 참조에 의한 호출

WBOY
풀어 주다: 2024-09-03 15:26:32
원래의
992명이 탐색했습니다.

참조 매개변수는 변수의 메모리 위치를 참조하는 데 사용됩니다. 매개변수에 대한 새 저장 위치가 필요한 값 매개변수와 달리 참조 매개변수는 메서드에 인수로 전달된 원래 매개변수와 동일한 메모리 위치를 나타냅니다. C#에서는 "ref"라는 키워드를 사용하여 참조 매개변수를 선언합니다. C#에서 참조 인수를 함수에 매개 변수로 전달할 때 원래 값 대신 메모리 위치에 대한 참조를 전달합니다. C#에서는 이 개념을 "참조에 의한 호출"이라고 부릅니다.

구문

ref data_typevariable_name
로그인 후 복사

여기서 data_type은 변수 이름이 있는 변수의 데이터 유형입니다.

참조에 의한 C# 호출 작업

  • 인수를 받는 함수를 호출해야 할 때마다 메모리 위치에 있는 변수의 참조 주소를 함수에 대한 인수로 전달하려는 경우 C#에서는 참조별 호출을 사용합니다.
  • 참조 매개변수는 메모리에 있는 변수의 주소를 전달받는 매개변수입니다.

다음은 언급된 예입니다.

예시 #1

숫자의 제곱을 계산하고 참조로 함수를 호출하기 전과 함수가 호출된 후에 값을 표시하는 참조에 의한 호출을 보여주는 C# 프로그램입니다.

코드:

using System;
//a namespace called program1 is defined
namespace program1
{
//a class called check is defined
class check
{
//a function is defined which takes reference variable as an argument
public void displaypower(ref double value)
{
//the square of the passed value is found using pow method
double power = Math.Pow(value,2);
//The resulting value is added to the value passed as reference
value = value + power;
Console.WriteLine("Value when the control is inside the function "+value);
}
//main method is called
static void Main(string[] args)
{
//a double variable is defined
double value = 5;
//an instance of the check class is defined which consists of the function taking reference parameter as an argument
check check1 = new check();
Console.WriteLine("Value before the function is called "+value);
//a function is called by reference
check1.displaypower(ref value);
Console.WriteLine("The value of the variable remains the same as inside the function because we are calling the function by reference " + value);
}
}
}
로그인 후 복사

출력:

C# 참조에 의한 호출

설명:

  • 주어진 프로그램에서 네임스페이스 “program1”을 정의합니다. 이 네임스페이스 안에는 함수를 포함하는 "check"라는 클래스가 있습니다. 이 함수는 참조 변수를 인수로 사용합니다.
  • 이 함수는 전달된 값을 참조로 사용하여 값의 제곱을 계산하여 실제 값에 더한 다음 결과 값을 표시합니다.

예시 #2

참조로 함수를 호출하고 소문자로 된 문자열을 참조 매개변수로 전달하여 주어진 소문자 문자열을 대문자로 변환하는 참조별 호출을 보여주는 C# 프로그램입니다.

코드:

using System;
//a namespace called program1 is defined
namespace program1
{
//a class called check is defined
class check
{
//a function is defined which takes reference variable as an argument
public void displayupper(ref string value)
{
//ToUpper method is used to convert the string from small letters to capital letters
value = value.ToUpper();
Console.WriteLine("Value when the control is inside the function "+value);
}
//main method is called
static void Main(string[] args)
{
//a double variable is defined
string value = "shobha";
//an instance of the check class is defined which consists of the function taking reference parameter as an argument
check check1 = new check();
Console.WriteLine("Value before the function is called "+value);
//a function is called by reference
check1.displayupper(ref value);
Console.WriteLine("The value of the variable remains the same as inside the function because we are calling the function by reference " + value);
}
}
}
로그인 후 복사

출력:

C# 참조에 의한 호출

설명:

  • 위 프로그램에서는 “program1”이라는 네임스페이스를 정의합니다. 그런 다음 이 네임스페이스 내에 "check"라는 클래스를 정의합니다. "check" 클래스 내부에는 참조 변수를 인수로 사용하는 함수를 정의합니다.
  • 이 함수는 전달된 값을 참조로 사용하고 ToUpper() 메서드를 사용하여 소문자로 지정된 문자열을 대문자로 변환합니다.

추천기사

C# Call By Reference에 대한 안내입니다. 여기서는 작업 및 프로그래밍 예제를 통해 소개를 논의합니다. 자세한 내용은 다음 기사를 참조하세요.

  1. C# 사전
  2. 데이터리더 C#
  3. IEnumerable C#
  4. C# 시스템.IO

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

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!