TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Out Parameter

C# Out Parameter for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C

C# Out Parameter

C# provides out keyword to pass arguments as out-type. It is like reference-type, except that it does not require variable to initialize before passing. We must use out keyword to pass argument as out-type. It is useful when we want a function to return multiple values.

C# Out Parameter Example 1

using System;
namespace OutParameter
{
    class Program
    {
        // User defined function
        public void Show(out int val) // Out parameter
        {
            int square = 5;
            val = square;
            val *= val; // Manipulating value
        }
        // Main function, execution entry point of the program
        static void Main(string[] args)
        {
            int val = 50;
            Program program = new Program(); // Creating Object
            Console.WriteLine("Value before passing out variable " + val);
            program.Show(out val); // Passing out argument
            Console.WriteLine("Value after recieving the out variable " + val);
        }
    }
}

Output:

Value before passing out variable 50
Value after receiving the out variable 25

The following example demonstrates that how a function can return multiple values.

C# Out Parameter Example 2

using System;
namespace OutParameter
{
    class Program
    {
        // User defined function
        public void Show(out int a, out int b) // Out parameter
        {
            int square = 5;
            a = square;
            b = square;
            // Manipulating value
            a *= a; 
            b *= b;
        }
        // Main function, execution entry point of the program
        static void Main(string[] args)
        {
            int val1 = 50, val2 = 100;
            Program program = new Program(); // Creating Object
            Console.WriteLine("Value before passing \n val1 = " + val1+" \n val2 = "+val2);
            program.Show(out val1, out val2); // Passing out argument
            Console.WriteLine("Value after passing \n val1 = " + val1 + " \n val2 = " + val2);
        }
    }
}

Output:

Value before passing
 val1 = 50
 val2 = 100
Value after passing
 val1 = 25
 val2 = 25

Next TopicC# Arrays




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf