locked
Method overloading in C#.NET

    Question

  • Is it possible Method Overloading in C# like it supports in VB.NET?
    Sunday, December 04, 2011 8:31 AM

Answers

  • Thank you.

    I would want to know that the way VB.NET provide Optional Keyword as a part of function parameter, what can be alternatives in C# .NET 3.5 (Except Method Overloading)?

    This is supported in C# 4.0 - but not in C# 3 (VS 2008).  In C# 4, you can do it as shown by Horizon_Net. 

     

    You can, technically, compile this that way (by manually entering the Optional and DefaultValue attributes).  That will cause VB to recognize the optional attributes, but C# (3.0) will still require overloaded methods.

     

    If you want this capability in C#, I'd recommend upgrading to C# and .NET 4 (VS 2010).

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, December 04, 2011 7:36 PM
  • Is it possible Method Overloading in C# like it supports in VB.NET?

    It is 100% the same.

     


    Success
    Cor
    Sunday, December 04, 2011 8:37 AM
  • Hi,

    you can do something like this:

    public void DoSomething(int required, string optionalString = "default string", int optionalInt = 10)
    {
        ...
    }
    
    ...and you can call this method in the following way:

    DoSomething(3, optionalInt: 4);
    DoSomething(3, "string", 4);
    DoSomething(3, optionalString: "string");
    


    Best Regards. Please Mark Answered, If my solution solves your problem.
    Sunday, December 04, 2011 11:36 AM
  • Thank you.

    I would want to know that the way VB.NET provide Optional Keyword as a part of function parameter, what can be alternatives in C# .NET 3.5 (Except Method Overloading)?

    Hi,
    
    see: http://msdn.microsoft.com/en-US/library/8f1hz171(v=VS.100).aspx
    
    and:
    
    http://msdn.microsoft.com/en-us/library/618ayhy6(v=VS.100).aspx
    
    Regards,
      Thorsten
    Sunday, December 04, 2011 2:50 PM

All replies

  • Is it possible Method Overloading in C# like it supports in VB.NET?

    It is 100% the same.

     


    Success
    Cor
    Sunday, December 04, 2011 8:37 AM
  • Thank you.

    I would want to know that the way VB.NET provide Optional Keyword as a part of function parameter, what can be alternatives in C# .NET 3.5 (Except Method Overloading)?

    Sunday, December 04, 2011 9:10 AM
  • Hi,

    you can do something like this:

    public void DoSomething(int required, string optionalString = "default string", int optionalInt = 10)
    {
        ...
    }
    
    ...and you can call this method in the following way:

    DoSomething(3, optionalInt: 4);
    DoSomething(3, "string", 4);
    DoSomething(3, optionalString: "string");
    


    Best Regards. Please Mark Answered, If my solution solves your problem.
    Sunday, December 04, 2011 11:36 AM
  • Thank you.

    I would want to know that the way VB.NET provide Optional Keyword as a part of function parameter, what can be alternatives in C# .NET 3.5 (Except Method Overloading)?

    Hi,
    
    see: http://msdn.microsoft.com/en-US/library/8f1hz171(v=VS.100).aspx
    
    and:
    
    http://msdn.microsoft.com/en-us/library/618ayhy6(v=VS.100).aspx
    
    Regards,
      Thorsten
    Sunday, December 04, 2011 2:50 PM
  • Thank you.

    I would want to know that the way VB.NET provide Optional Keyword as a part of function parameter, what can be alternatives in C# .NET 3.5 (Except Method Overloading)?

    This is supported in C# 4.0 - but not in C# 3 (VS 2008).  In C# 4, you can do it as shown by Horizon_Net. 

     

    You can, technically, compile this that way (by manually entering the Optional and DefaultValue attributes).  That will cause VB to recognize the optional attributes, but C# (3.0) will still require overloaded methods.

     

    If you want this capability in C#, I'd recommend upgrading to C# and .NET 4 (VS 2010).

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, December 04, 2011 7:36 PM
  • hi

    in C#.NET you can change parameter and/or return type of a method for get overload of a method

    like this:

         public List<string> YourMethodName()
        {
              //implementation here
        }
       
        public List<int> YourMethodName(int count)
        {
              //implementation here
        }

    Sunday, December 04, 2011 9:19 PM