当前位置:网站首页>C method parameter: out
C method parameter: out
2022-06-13 03:07:00 【cathy18c】
out keyword , Only modify but not read , in other words , When an argument is passed to a formal parameter , Arguments cannot be initialized , Pay attention to whether it is necessary or not
int initializeInMethod;
OutArgExample(out initializeInMethod);
Console.WriteLine(initializeInMethod); // value is now 44
void OutArgExample(out int number)
{
number = 44;
}You can't give initializeInMethod Assign initial value to , Modify it within the method , After the method is over , This variable initializeInMethod Automatic value , no need return
Follow ref in equally , It is also used for both defining methods and invoking methods out
On method overloading and ref in equally , If everything else is the same , Only the signature of the method has ref in out distinguish , It is impossible to judge that they are not a method , as follows :
class CS0663_Example
{
// Compiler error CS0663: "Cannot define overloaded
// methods that differ only on ref and out".
public void SampleMethod(out int i) { }
public void SampleMethod(ref int i) { }
}SampleMethod Method return value type , Method name , Parameter type , Number , The order , All the same , Only different modifiers , The system still recognizes them as a method , So there's a compilation error .
The following is true :
class OutOverloadExample
{
public void SampleMethod(int i) { }
public void SampleMethod(out int i) => i = 5;
}Statement out Parameters
Use out The parameter declaration method is a classic solution for returning multiple values
void Method(out int answer, out string message, out string stillNull)
{
answer = 44;
message = "I've been returned";
stillNull = null;
}
int argNumber;
string argMessage, argDefault;
Method(out argNumber, out argMessage, out argDefault);
Console.WriteLine(argNumber);
Console.WriteLine(argMessage);
Console.WriteLine(argDefault == null);
// The example displays the following output:
// 44
// I've been returned
// TrueCall with out Parameter method
The original way of writing
string numberAsString = "1640";
int number;
if (Int32.TryParse(numberAsString, out number))
Console.WriteLine($"Converted '{numberAsString}' to {number}");
else
Console.WriteLine($"Unable to convert '{numberAsString}'");
// The example displays the following output:
// Converted '1640' to 1640from C# 7.0 Start , It can be declared in the parameter list of the method call rather than in a separate variable declaration out Variable . This makes the code more concise and readable , It also prevents inadvertent assignment of values to the variable before the method call . The following example is basically the same as the previous one , The difference is that it is right Int32.TryParse Method is defined number Variable .
string numberAsString = "1640";
if (Int32.TryParse(numberAsString, out int number))
Console.WriteLine($"Converted '{numberAsString}' to {number}");
else
Console.WriteLine($"Unable to convert '{numberAsString}'");
// The example displays the following output:
// Converted '1640' to 1640In the previous example ,number Variables are strongly typed to int. You can also declare an implicitly typed local variable , This is shown in the following example .
string numberAsString = "1640";
if (Int32.TryParse(numberAsString, out var number))
Console.WriteLine($"Converted '{numberAsString}' to {number}");
else
Console.WriteLine($"Unable to convert '{numberAsString}'");
// The example displays the following output:
// Converted '1640' to 1640
Reference resources :out Parameter modifiers
边栏推荐
- My practice of SOA architecture project based on WCF
- PK of dotnet architecture
- Hash table: valid anagrams
- HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
- Capital digit to number format
- Control scanner in Delphi
- Pycharm and Anaconda ultra detailed installation and configuration tutorial
- MySQL transactions and locks (V)
- Retrofit easy to use
- The weight of the input and textarea components of the applet is higher than that of the fixed Z-index
猜你喜欢

Use of jstack

二叉樹初始化代碼

Operating principle of JS core EventLoop

Data warehouse notes | 5 factors that need attention for customer dimension modeling

Image classification system based on support vector machine (Matlab GUI interface version)

Use and arrangement of wechat applet coordinate position interface (I)

【pytorch 記錄】pytorch的變量parameter、buffer。self.register_buffer()、self.register_parameter()

Linked list: orderly circular linked list

Linked list: palindrome linked list

JVM class loading (I)
随机推荐
Prometheus install and register services
Summary of the latest IOS interview questions in June 2020 (answers)
mysql索引
In my ten years, every bit has become a landscape?
Use of jstack
Installing the IK word breaker
IOS development internal volume interview questions
Radio design and implementation in IVI system
Linked list: the first coincident node of two linked lists
Ijkplayer source code ---setdatasource
wx. Createselectorquery() gets the usage of DOM nodes in components
wx.createSelectorQuery()在components获取Dom节点的使用
Exercise 8-3 rotate array right
Review notes of RS data communication foundation STP
Retrofit easy to use
C 10 new features_ C 10 new features
How to manage the IT R & D department?
Kotlin memorandum
P1048 [noip2005 popularization group] Drug collection
Understanding of intermediatelayergetter