当前位置:网站首页>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
// True
Call 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 1640
from 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 1640
In 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
边栏推荐
- Ijkplayer source code -- Library loading and initialization
- Keil removes annoying st link update tips
- Review notes of RS data communication foundation STP
- Use of interceptors webmvcconfigurer
- IOS development internal volume interview questions
- C# . NET ASP. Net relationships and differences
- How to select fund products? What kind of fund is a good fund?
- Delete the number of a range in the linked list
- C # simple understanding - static members and static classes
- C 10 new features_ C 10 new features
猜你喜欢
Installing the IK word breaker
Prometheus node_ Exporter installs and registers as a service
Typical application of ACL
JVM GC (V)
Data warehouse notes | 5 factors that need attention for customer dimension modeling
Scala implements workcount
Introduction to redis (using redis, common commands, persistence methods, and cluster operations)
Graduation project - campus old thing recycling system based on stm32
Coordinate location interface of wechat applet (II) map plug-in
Binary tree initialization code
随机推荐
The extra money we made in those years
【pytorch 記錄】pytorch的變量parameter、buffer。self.register_buffer()、self.register_parameter()
Introduction and download of common data sets for in-depth learning (with network disk link)
Use of jstack
Introduction to facial expression recognition system -- offline environment configuration
Techniques for collecting stringgrid
C# . NET ASP. Net relationships and differences
Wechat applet switch style rewriting
Ijkplayer source code -- Library loading and initialization
PK of dotnet architecture
Sword finger offer2: queue summary
Stack: daily temperature
Use of interceptors webmvcconfigurer
Delete the number of a range in the linked list
Using binary heap to implement priority queue
Open source - campus forum and resource sharing applet
Hash tables: metaphrases
六款国产飞机专用GPU产品通过鉴定审查
Introduction to facial expression recognition system - Technical Paper Edition
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)