Can two methods have same name with different return types in c#?

return type is different. Method name & argument list same.

  public class MyClass
    {
        public double Test(int x)
        {
            return x * 120;       
        }
        public int Test(int x)
        {
            return x * 120;
        }
   }
     


    class Program
    {
        static void Main(string[] args)
        {
                  
            MyClass obj = new MyClass();
            obj.Test(12);
            Console.WriteLine();
            Console.ReadLine();
        }

    } 
It will throw a compilation error: 
More than one method with same name and argument list cannot be given in a class even though their return type is different.
Method return type doesn't matter in case of overloading.

Comments

Popular posts from this blog

Validate Mobile Number with 10 Digits in ASP.Net