out (C# Reference)
You can use the out
keyword in two contexts:
As a parameter modifier, which lets you pass an argument to a method by reference rather than by value.
In generic type parameter declarations for interfaces and delegates, which specifies that a type parameter is covariant.
The out
keyword is especially useful when a method needs to return more than one value since more than one out
parameter can be used e.g.
public void Main()
{
double radiusValue = 3.92781;
//Calculate the circumference and area of a circle, returning the results to Main().
CalculateCircumferenceAndArea(radiusValue, out double circumferenceResult, out areaResult);
System.Console.WriteLine($"Circumference of a circle with a radius of {radiusValue} is {circumferenceResult}.");
System.Console.WriteLine($"Are of a circle with a radius of {radiusValue} is {areaResult}.");
Console.ReadLine();
}
//The calculation worker method.
public static void CalculateCircumferenceAndArea(double radius, out double circumference, out double area)
{
circumference = 2 * Math.PI * radius;
area = Math.PI * (radius * radius);
}
The following limitations apply to using the out
keyword:
out
parameters are not allowed in asynchronous methods.out
parameters are not allowed in iterator methods.- Properties cannot be passed as
out
parameters.
Cộng tác với chúng tôi trên GitHub
Bạn có thể tìm thấy nguồn cho nội dung này trên GitHub, nơi bạn cũng có thể tạo và xem lại các vấn đề và yêu cầu kéo. Để biết thêm thông tin, hãy xem hướng dẫn dành cho người đóng góp của chúng tôi.
Phản hồi
https://aka.ms/ContentUserFeedback.
Sắp ra mắt: Trong năm 2024, chúng tôi sẽ dần gỡ bỏ Sự cố với GitHub dưới dạng cơ chế phản hồi cho nội dung và thay thế bằng hệ thống phản hồi mới. Để biết thêm thông tin, hãy xem:Gửi và xem ý kiến phản hồi dành cho