What is out keyword in C#?
What is out keyword in C#?
Student
Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies. I love to learn new things in life that keep me motivated.
C# out Keyword
In C#, the out keyword is used to pass arguments to methods by reference instead of by value. This allows the method to change the value of the argument and that change is reflected outside the method. It is especially useful when you need a way to return multiple values.
Here are some examples to illustrate the use of the out keyword,
Key Points
Parameter Declaration-
outkeyword must be specified in the method declaration and method calls.outdoes not need to be initialized before it is passed to the method, but a value must be provided in the method before it returns.Method Signature-
outkeyword for any parameters to be passed by reference.Multiple Return Values-
outparameters are often used to return multiple values from a method.Using
outto return multiple valuesOutput-
If enter divisor value 0 (zero)
Using
outwithTryParseOutput 1-
Output 2-
The out keyword provides a way to return multiple values from a single method, increasing the simplicity and efficiency of your method in C#.
Also Read: How to convert string into int in C#?