Friday, 1 September 2017

How to Use Out Parameter In C# With Example

How to Use Out Parameter In C# With Example

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); 
            } 
        } 
    } 

    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); 
                } 
            } 
        } 

Post a Comment

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search