In: Computer Science
1. Why can reference parameters not be optional parameters?
Reference parameters
We use reference parameter when we want to change the value of variable in the called method and want it to be reflected in calling method, The reference keyword is used to pass an argument as a reference. when an argument is passed using a reference keyword, it must be initialized in the calling method before it is passed to the called method and if you have not initialized it prior to calling it will not work.
public static int square(ref int num)
{
int result = num * num;
num++;
return result;
}
static void Main(string[] args)
{
int number = 10;
Console.WriteLine("square of n value:" + square(ref number));
Console.WriteLine("Current value of n: {0}", number);
}
Optional parameter - As the name suggests optional parameters are not compulsory parameters, they are optional. It helps to exclude arguments for some parameters. Or we can say in optional parameters, it is not necessary to pass all the parameters in the method.
using System;
class Program
{
static void Main()
{
printDetails (“Krishna”, 21); // prints name : Krishna, age : 21
// You can omit the age parameter, then age will be 25
printDetails (“Krishna”); // prints name : Krishna, age : 25
}
static void printDetails(string name, int age = 25)
{
Console.WriteLine("name = {0}, age = {1}", name, age);
}
}
2. What are the conditions for generating ambiguous methods?
The conditions for generating ambiguous methods are when an overloaded method is called and the interpreter cannot determine which method should be used. What this means is that overloaded methods are not ambiguous by themselves, but they become ambiguous when there is an ambiguous situation.
3. What are the differences between mandatory parameters and optional parameters?
Each parameter has a default value, which is used if no parameter is specified on the command line. A parameter may be either mandatory or optional. The default value and mandatory attribute are specified in the parameter file.
A mandatory parameter must be explicitly given on the command-line, otherwise an error message will be printed to prompt the user to re-enter the command. If an optional parameter is not specified, the default is used.
4. Explain the use of arguments in reference parameters, output parameters, and parameter arrays.
Use of arguments in reference parameters
The call by refernce method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.
Use of output parameters
A return statement can be used for returning only one value from a function. However, using output parameters, you can return two values from a function. Output parameters are similar to reference parameters, except that they transfer data out of the method rather than into it.
Use of parameter arrays
When you need an indefinite number of arguments, you can declare a parameter array, which allows a procedure to accept an array of values for a parameter. You do not have to know the number of elements in the parameter array when you define the procedure
5. Expalin overloading.
Method Overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. A user can implement function overloading by defining two or more functions in a class sharing the same name. C# can distinguish the methods with different method signatures. i.e. the methods can have the same name but with different parameters list (i.e. the number of the parameters, order of the parameters, and data types of the parameters) within the same class.
Different ways of doing overloading
methods-
Method overloading can be done by changing: