ADVERTISEMENT
ADVERTISEMENT

C#.Net find output programs (default Arguments) | set 1

Find the output of C#.Net programs | default Arguments | Set 1: Enhance the knowledge of C#.Net default Arguments concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 07, 2021

Question 1:

using System;

namespace Demo
{
    class Program
    {
        static void fun(int a, int b, int c = 30)
        {
            Console.WriteLine("A: " + a + ", B: " + b + ", C: " + c);
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            fun(10, 20, 40);
            fun(10, 20);
        }
    }
}   

Output:

A: 10, B: 20, C: 40
A: 10, B: 20, C: 30
Press any key to continue . . .

Explanation:

In the above program, we create a class Program that contains two static methods fun() and Main().

The method fun() contains three arguments a, b, and c. Here, argument c is used as a default argument. It means if we did not pass the value 3rd argument in the method fun(), then it takes value 30 for argument c by default.

Now coming to the Main() method, this is an entry point for program execution.

fun(10, 20, 40);

The above method call will print "A: 10, B: 20, C: 40" on the console screen.

fun(10, 20);

The above method call will print "A: 10, B: 20, C: 30" on the console screen.

Question 2:

using System;

namespace Demo
{
    class Program
    {
        static void fun(int a, int b=50, int c)
        {
            Console.WriteLine("A: " + a + ", B: " + b + ", C: " + c);
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            fun(10, 20, 40);
            fun(10, c: 20);
        }
    }
}

Output:

main.cs(7,46): error CS1737: Optional parameter cannot precede required parameters

Explanation:

The above program will generate syntax error because default parameters must be trailing parameters in C#.

Question 3:

using System;

namespace Demo
{
    class Program
    {
        static void fun(int a, int b=50, int c=80)
        {
            Console.WriteLine("A: " + a + ", B: " + b + ", C: " + c);
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            fun(10, 20, 40);
            fun(10, c: 20);
        }
    }
}

Output:

A: 10, B: 20, C: 40
A: 10, B: 50, C: 20
Press any key to continue . . .

Explanation:

In the above program, we create a class Program that contains two static methods fun() and Main().

The method fun() contains three arguments a, b, and c. Herem arguments b and c are used as default arguments. It means if we did not pass the values of 2nd or 3rd argument in the method fun(), then it takes use default values.

Now coming to the Main() method, this is an entry point for program execution,

fun(10, 20, 40);

The above method call will print "A: 10, B: 20, C: 40" on the console screen.

fun(10, c:20);

The above method call will print "A: 10, B: 50, C: 20" on the console screen. Here, we used the default value of argument b.

Question 4:

using System;

namespace Demo
{
    class Program
    {
        static void fun(int a, int b=50, int c=80)
        {
            Console.WriteLine("A: " + a + ", B: " + b + ", C: " + c);
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            fun(10, 20, 40);
            fun(10, b: 20);
        }
    }
}

Output:

A: 10, B: 20, C: 40
A: 10, B: 20, C: 80
Press any key to continue . . .

Explanation:

In the above program, we create a class Program that contains two static methods fun() and Main().

The method fun() contains three arguments a, b, and c. Here, arguments b and c are used as default arguments. It means if we did not pass the values of 2nd or 3rd argument in the method fun(), then it takes use default values.

Now coming to the Main() method, this is an entry point for program execution,

fun(10, 20, 40);

The above method call will print "A: 10, B: 20, C: 40" on the console screen,

fun(10, b:20);

The above method call will print "A: 10, B: 20, C: 80" on the console screen. Here, we used the default value of argument c.

Question 5:

using System;

namespace Demo
{
    class Program
    {
        const int VAL = 10;

        static void fun(int a, int b=50, int c=VAL)
        {
            Console.WriteLine("A: " + a + ", B: " + b + ", C: " + c);
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            fun(10, 20, 40);
            fun(10, b: 20);
        }
    }
}

Output:

A: 10, B: 20, C: 40
A: 10, B: 20, C: 10
Press any key to continue . . .

Explanation:

In the above program, we create a class Program that contains two static methods fun() and Main() and we also defined a constant VAL that contains value 10, which is used as a default value for argument c in fun() method.

The method fun() contains three arguments a, b, and c. Here, arguments b and c are used as default arguments. It means if we did not pass the values of 2nd or 3rd argument in the method fun(), then it takes use default values.

Now coming to the Main() method, this is an entry point for program execution,

fun(10, 20, 40);

The above method call will print "A: 10, B: 20, C: 40" on the console screen,

fun(10, b:20);

The above method call will print "A: 10, B: 20, C: 10" on the console screen. Here, we used the default value of argument c.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.