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

The String.Join Method in C# Explained

The String.Join Method in C#   The string.Join concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. Overloads of string.Join Method Description Join(Char, Object[]) Concatenates the string representations of an array of objects, using the specified separator between each member. Join(Char, String[]) Concatenates an array of strings, using the specified separator between each member. Join(String, IEnumerable<String>) Concatenates the members of a constructed IEnumerable<T> collection of type String, using the specified separator between each member. Join(String, Object[]) Concatenates the elements of an object array, using the specified separator between each element. Join(String, String[]) Concatenates all the elements of a string array, usi...

Most Popular Programming Languages in 2020

Most Popular Programming Languages in 2020 In this blog post, you will learn about the most popular programming languages in 2020 for creating the best web applications. Check its pros and cons. Analyzed by technostacks Not very long ago, just a few people were considered to be computer programmers, and the general public viewed them with awe. In this digital age that we are now living in, however, a large number of IT jobs need a solid grasp of one or more programming languages. Whether one wants to develop a mobile app or get a certification for having programming knowledge, or even to learn new skills, one needs to opt for the right programming language. Below mentioned eight most popular programming languages which are in demand for software development and web applications. This is the most used programming languages in 2019 and will be in 2020. For each, there is little information about the language, benefits and its complexity, as well as about its usage. One must...

HashTable in C# with Example

  HashTable in C# with Example Hashtable  is used to store a collection of key/value pairs of different  data types  and are organized based on the hash code of the key.   Generally, the hashtable object will contain buckets to store elements of the collection. The bucket here, is a virtual subgroup of elements within the hashtable and each bucket is associated with a hash code, which is generated based on the key of an element.   In C#, hashtable is same as a  dictionary  object but the only difference is that the  dictionary  object is used to store a key-value pair of same  data type  elements.   When compared with  dictionary  object, the hashtable will provide a lower performance because the hashtable elements are of object type so the boxing and unboxing process will occur when we are storing or retrieving values from the hashtable.   C# HashTable Declaration Hashtable is a non-generic type...