The Out Parameter with Example in C#
In C#, out keyword
is used to pass arguments to the method as a reference type. The out keyword
same as the ref keyword but the only difference is out doesn’t
require a variable to be initialized before we pass it as an argument to the
method but the variable must be
initialized in called method before it returns a value to the calling method. The
out parameter in C# is also useful to return more than one value from methods.
Declaration of C# Out Parameter
Following is a simple
example of using out parameters in C#.
OutParameterMultTest(out x);
From the above declaration, we just
declared a variable x and
pass it to the method by using out parameter
without assigning any value but as discussed, the variable must be initialized
in called method before it returns a value to the calling method.
To use out parameter
in C# application, both the method definition and the calling method must
explicitly use the out keyword.
Out Parameter Example in C#
The following is an example of passing
an out parameter to a
method in C#.
using System;
namespace CsharpnaijaTutorial
{
class Program
{
static void Main(string[] args)
{
int x;
OutTestMult(out x);
Console.WriteLine($"Variable Value: {x}");
Console.WriteLine("Press Enter Key to
Exit..");
Console.ReadLine();
}
public static void OutTestMult (out int a)
{
a = 10;
a *= a;
}
}
}
Examining the above example, we declared
a variable x and
passing it to a OutTestMult method by using out keyword
without initializing the value. However, the called method (OutTestMult) is initializing the value before
returning the value to the calling method.
Multiple Out Parameters Example
The following
is an example of using multiple out parameters
in C#.
using System;
namespace CsharpnaijaTutorial
{
class Program
{
static void Main(string[] args)
{
int x, y;
OutTestMult(out x, out y);
Console.WriteLine($"x Value: {x}");
Console.WriteLine($"y Value: {y}");
Console.WriteLine("Press Enter Key to
Exit..");
Console.ReadLine();
}
public static void OutTestMult(out int a, out int b)
{
a = 20;
b = 15;
a *= a;
b *= b;
}
}
}
The output of the above code sample is
as shown below
References
1.
Tutlane
2.
CsharpCorner
Great post. Articles that have meaningful and insightful comments are more enjoyable. 사설토토
ReplyDeleteVery great post. I just stumbled upon your blog and wanted to say that I have truly loved browsing your blog posts.
ReplyDeleteSign up for free. Easy to apply at this link: 카지노
I love reading this site. This was really very informative site for me. I really liked it. This was really a cordial post. Thanks a lot! 온라인카지노
ReplyDeleteThis article is very interesting and effective. Thank you and good luck in the upcoming articles.
ReplyDelete온라인카지노
I felt very happy while reading this site. This was really very informative site for me. 카지노사이트
ReplyDeleteKeep up the great writing. Thankyou for sharing. 바카라사이트
ReplyDeleteThanks for such a valuable post.
ReplyDelete카지노사이트
Its an amazing website, I really enjoy reading your articles.
ReplyDelete카지노사이트
It is always so lovely and jam-packed with a great time
ReplyDelete바카라사이트
I really like the website you share on us .I appreciate the effort you made. Keep on blogging..
ReplyDeleteIf you jumble everything together, trying to get the most material you can get on one page, readers will become overwhelmed. 토토사이트
ReplyDeleteIf you jumble everything together, trying to get the most material you can get on one page, readers will become overwhelmed. 토토사이트
ReplyDeleteThat’s a great article! The neatly organized content is good to see. Can I quote a blog and write it on my blog? My blog has a variety of communities including these articles. Would you like to visit me later?
ReplyDelete
ReplyDeleteThanks for such a valuable post. I am waiting for your next post, I have enjoyed a lot reading this post keep it up.
카지노사이트핫
바둑이사이트넷
토토사이트
Hi there! I could have sworn I’ve been to this site before but after browsing through some of the articles I realized it’s new to me.
ReplyDelete토토사이트
바카라사이트
토토사이트
I am really happy that you writing content article like this. its greattt Thanksss
ReplyDelete바카라사이트
카지노사이트
온라인카지노
바카라사이트닷컴
It’s a great content! Also your web site loads up very fast!
ReplyDelete배트맨토토
It is in reality a nice and useful piece of information. Thank you for sharing.
ReplyDelete카지노사이트
온라인카지노
바카라사이트
카지노사이트킹
I think this is among the most vital info for me.
ReplyDeleteAnd i am glad reading your article.
토토
카지노사이트
바카라
토토