Question

In: Computer Science

C# Discussion question Why can reference parameters not be optional parameters? What are the conditions for...

  • C# Discussion question
  • Why can reference parameters not be optional parameters?
  • What are the conditions for generating ambiguous methods?
  • What arethe differences between mandatory parameters and optional parameters?
  • Explain the use of arguments in reference parameters, output parameters, and parameter arrays.
  • Expalin overloading.

Solutions

Expert Solution

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.

  • You can use optional parameters in Methods, Constructors, Indexers, and Delegates.
  • Each and every optional parameter contains a default value which is the part of its definition.
  • If we do not pass any parameter to the optional arguments, then it takes its default value.
  • The default value of an optional parameter is a constant expression.
  • The optional parameters are always defined at the end of the parameter list. Or in other words, the last parameter of the method, constructor, etc. is the optional parameter.

    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:

  1. The number of parameters in two methods.
  2. The data types of the parameters of methods.
  3. The Order of the parameters of methods.

Related Solutions

Why can reference parameters not be optional parameters? What are the conditions for generating ambiguous methods?
Why can reference parameters not be optional parameters? What are the conditions for generating ambiguous methods?
Write a function that will accept two integer matrices C and D by reference parameters. The...
Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks] Explain the time complexity of this function inside of the function code as a comment. [3 marks] in C++
C language Modify the getAvg() function to support passing an array by reference. The parameters of...
C language Modify the getAvg() function to support passing an array by reference. The parameters of this function should be pointer to a float array, and the size of the array. Returns the average of the array. Build a function called getScoreFromUser() with the following properties: Asks the user to enter the score scans into a local float variable returns the float value that the user entered. Build and present a menu to a user like the following and enclose...
Write in C++ Print the following heading (should be in heading function) Welcome to Reference parameters...
Write in C++ Print the following heading (should be in heading function) Welcome to Reference parameters 2 ) ask the user for 2 numbers (should be in main) 3 ) Calculate the sum and product of the numbers using reference parameters (should be in getAnswer function) 4 ) Print the sum and product of the numbers in the following format: (should be in printmessage function) Here is the function you need        Function name | Parameters                       | Return Type    heading |...
What are returns to scale? Under what conditions (that is, for what values of the parameters...
What are returns to scale? Under what conditions (that is, for what values of the parameters a and b) does the Cobb-Douglas production function,q = KaLb, exhibit constant and increasing returns to scale? (Hint: See Solved Problem 6.3.)
What are the conditions on the parameters for linear multistep methods so that the methods are...
What are the conditions on the parameters for linear multistep methods so that the methods are regular? Would appreciate a detailed explanation.
C# Answer in discussion format Why is an array such a useful tool? What is a...
C# Answer in discussion format Why is an array such a useful tool? What is a jagged array? Ask your students to describe some circumstances in which jagged arrays might be useful. the System.Array class contains a variety of useful, built-in methods, describe one of them and how you would use it. Describe how to resize an array in C# using the new operator. How would you declare an array if you do not know in advance how many elements...
Write 1 to 3 paragraphs to discussion this question What labor standards regarding safety, working conditions,...
Write 1 to 3 paragraphs to discussion this question What labor standards regarding safety, working conditions, overtime, and the like, should Nike hold foreign factories to: those prevailing in that country, those prevailing in the United States, or something in between?
Why is S. aureus such a virulent organism? What conditions can it cause? What about the...
Why is S. aureus such a virulent organism? What conditions can it cause? What about the organism can lead to so many different kinds of infection/disease?
What is a good reference for Why prostatitis and infection happens? The reference need to be...
What is a good reference for Why prostatitis and infection happens? The reference need to be after 2016.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT