C# Programming - Constructors - Discussion

Discussion :: Constructors - General Questions (Q.No.2)

2. 

Which of the following statements is correct about the C#.NET code snippet given below?

namespace IndiabixConsoleApplication
{ 
    class Sample
    { 
        public int func()
        {
            return 1;
        } 
        public Single func()
        { 
            return 2.4f ;
        } 
    } 
    class Program
    { 
        static void Main(string[ ] args)
        {
            Sample s1 = new Sample(); 
            int i;
            i = s1.func(); 
            Single j; 
            j = s1.func(); 
        } 
    } 
}

[A]. func() is a valid overloaded function.
[B]. Overloading works only in case of subroutines and not in case of functions.
[C]. func() cannot be considered overloaded because: return value cannot be used to distinguish between two overloaded functions.
[D]. The call to i = s1.func() will assign 1 to i.
[E]. The call j = s1.func() will assign 2.4 to j.

Answer: Option C

Explanation:

No answer description available for this question.

Sorna Ranjani said: (Jun 22, 2011)  
Functions are declared and they are not defined. So func is not overloaded.

Gowtham said: (Nov 22, 2011)  
I think functions can be overloaded with the type and number of arguments.

Rajeev said: (Nov 29, 2011)  
Because in method overloading parameters.

Should be different, not return type.

Nishant Rai said: (Jan 7, 2012)  
Because in method overloading function name must be same as the class or struct name..and there should be either
1) different parameter
2) return type of parameter
3) sequence of parameter

Ammu said: (Apr 10, 2012)  
@rajiv well said

Mohammed Ansari said: (Jun 5, 2012)  
@Nishant Rai.
You are wrong as Only the name of the methods which are going to be overload need to be have same name, it is not necessary that the Method name should Match with the class or Struct Name.

Rajeev Answers is Highly Acceptable.

Sadiq said: (Aug 30, 2012)  
A function is said to be overloaded when the function is created with same name but with different signature.
Return type is not considered to be in method/funtion signature and hence declaring a function with same name and same signature leads to ambiguous situation for the complier to which function needs to executed when the function is called, hence it throws a compliation error.

Karthi said: (Feb 11, 2013)  
Somebody please give the correct definition for signature. Because all this while I have been thinking that it is both return type and arguments.

Mathew said: (Mar 3, 2013)  
Sadiq is quite right. As for definition of Signature, a method/function Signature is what appears in parenthesis right after the method/function name which represents the parameter(s) names(s) and their type(s).

And as Sadiq mentioned, Return Type of a method/function is not part of the Signature and has nothing to do with method/function overloading.

Qyam Uddin said: (Apr 6, 2013)  
Signature should include following:

No of parameter.
Type of parameter.
Order of parameter.

Jyoti said: (May 15, 2013)  
Function overloading is by defining two or more functions in a class sharing the same name. However, each definition of a function must differ in its function signature.

Amit Balan said: (Dec 17, 2013)  
Function overloading is a technique to implement polymorphism two or more functions within in a class having same name can be overloaded but those functions must differ in the number of parameter, type of parameter and sequence of parameter.

Ildaokan said: (May 27, 2014)  
I was more hooked up on "why D or E is not correct?"
When I try to compile the code on vs.

I got error like:

'TheApplication.Sample' already defines a member called 'func' with the same parameter types.

So I have tried the code just like :
###########################################
using System;

namespace denemeApp
{
class Sample
{
public int func()
{
return 1;
}
//public Single func()
//{
//return 2.4f ;
//}
}
class Program
{
static void Main(string[] args)
{
Sample s1 = new Sample();
int i;

i = s1.func();
Debug.WriteLine("Output to the program :"+s1.func());

}
}
}

################################
Maybe this is a basic idea but I have found it useful.

Piterskiy said: (Jan 20, 2015)  
The answers D and E cannot be correct since the code will not compile at all since overloading condition is not met.

Abhijit said: (Feb 26, 2015)  
Simple fundamental of overloading.

If you would like to overload a function then your function parameter list has to differ from each other.

Your signature can be same only in case of function overriding.

Overloading doesn't have to do anything with access modifier, return type.

Function overload (string name).

Function overload (ref name) or function overload (out name).

(anyone is valid for overload but not both at a time).

I hope I am clear.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.