C# Programming - Constructors - Discussion

Discussion Forum : 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(); 
        } 
    } 
}
func() is a valid overloaded function.
Overloading works only in case of subroutines and not in case of functions.
func() cannot be considered overloaded because: return value cannot be used to distinguish between two overloaded functions.
The call to i = s1.func() will assign 1 to i.
The call j = s1.func() will assign 2.4 to j.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
15 comments Page 2 of 2.

Jyoti said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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:   1 decade ago
The answers D and E cannot be correct since the code will not compile at all since overloading condition is not met.

Abhijit said:   1 decade ago
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:

Your comments will be displayed after verification.