Skip to main content

The Out Parameter with Example in C#

 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#.

 int x; // No need to initialize variable

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

out_parameter


 The C# nigeria tutorial out parameter article

References

1.     Tutlane

2.     CsharpCorner

3.     MicrosoftDocumentation

4.     Geeksfor Geeks

 

 

 

 

 

 

 

Comments

  1. Great post. Articles that have meaningful and insightful comments are more enjoyable. 사설토토

    ReplyDelete
  2. Very great post. I just stumbled upon your blog and wanted to say that I have truly loved browsing your blog posts.

    Sign up for free. Easy to apply at this link: 카지노

    ReplyDelete
  3. 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! 온라인카지노

    ReplyDelete
  4. This article is very interesting and effective. Thank you and good luck in the upcoming articles.
    온라인카지노

    ReplyDelete
  5. I felt very happy while reading this site. This was really very informative site for me. 카지노사이트

    ReplyDelete
  6. Keep up the great writing. Thankyou for sharing. 바카라사이트

    ReplyDelete
  7. Its an amazing website, I really enjoy reading your articles.
    카지노사이트

    ReplyDelete
  8. It is always so lovely and jam-packed with a great time
    바카라사이트

    ReplyDelete
  9. I really like the website you share on us .I appreciate the effort you made. Keep on blogging..

    ReplyDelete
  10. If you jumble everything together, trying to get the most material you can get on one page, readers will become overwhelmed. 토토사이트

    ReplyDelete
  11. If you jumble everything together, trying to get the most material you can get on one page, readers will become overwhelmed. 토토사이트

    ReplyDelete
  12. That’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

  13. Thanks for such a valuable post. I am waiting for your next post, I have enjoyed a lot reading this post keep it up.
    카지노사이트핫
    바둑이사이트넷
    토토사이트

    ReplyDelete
  14. 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
  15. It’s a great content! Also your web site loads up very fast!
    배트맨토토

    ReplyDelete
  16. I think this is among the most vital info for me.
    And i am glad reading your article.

    토토
    카지노사이트
    바카라
    토토

    ReplyDelete

Post a Comment

Popular posts from this blog

Classes in C# Explained

C# Class Explained A class is nothing but an encapsulation of properties and methods that are used to represent a real-time entity, as explained by Guru99 . For instance, if you want to work with Guest’s data as in our previous DataDriven Web application . The properties of the Guest would be the Id, GuestName, Address, Phone number etc of the Guest. The methods would include the entry and modification of Guest data. All of these operations can be represented as a class in C# as shown below. using System; namespace CsharpnaijaClassTutorial {     public class Guest     {         public int Id { get ; set ; }         public string GuestName { get ; set ; }         public string Address { get ; set ; }         public string WhomToSee { get ; set ; }     ...

ASP.NET MVC Views

Views in ASP.NET MVC Application explained Find a related article By  Steve Smith  and  Luke Latham from Microsoft Corporation here In the Model-View-Controller (MVC) pattern, the  view  handles the application's data presentation and user interaction. A view is an HTML template with embedded  Razor markup . Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client. In ASP.NET MVC, views are  .cshtml  files that use the  C# programming language  in Razor markup. Usually, view files are grouped into folders named for each of the application's  controllers . The folders are stored in a  Views  folder at the root of the application as shown: The  Home  controller is represented by a  Home  folder inside the  Views  folder.  The  Home  folder contains the views for the  About ,  Contact , and  Index...

ASP.NET MVC Routing

ASP.NET MVC Routing ASP.NET MVC routing is a pattern matching system that is responsible for mapping incoming browser requests to specified MVC controller actions. When the ASP.NET MVC application launches then the application registers one or more patterns with the framework's route table to tell the routing engine what to do with any requests that matches those patterns. When the routing engine receives a request at runtime, it matches that request's URL against the URL patterns registered with it and gives the response according to a pattern match. Routing pattern is as follows A URL is requested from a browser, the URL is parsed (that is, break into controller and action), the parsed URL is compared to registered route pattern in the framework’s route table, if a route is found, its process and send response to the browser with the required response, otherwise, the HTTP 404 error is send to the browser. Route Properties ASP.NET MVC routes are res...