What is method signature in C#? Explain.

Solution

C# allows us to define multiple methods in a class with the same name having different method signature. Method signature includes the following

  • Name of the method
  • Number of arguments in a method
  • Data type of the arguments

Note: return type of method in not part of method signature. param keyword is also not part of method signature.

Consider the below examples 

Example

class Student
{
   void TestMethod() { };                          // TestMethod() 
   void TestMethod(int x) { };                    // TestMethod(int)
   void TestMethod(ref int x) { };               // TestMethod(ref int)
   void TestMethod(int x, int y) { };            // TestMethod(int, int)
   int TestMethod(string s) { };                 // TestMethod(string)
   int TestMethod(int x) { };                     // TestMethod(int)  error as return type int is not considered as part of  method signature
   void TestMethod(string[] a) { };              // TestMethod(string[])
   void TestMethod(params string[] a) { };      //TestMethod(string[])      error as peram is not part of method signature
}

Share this thread

Comments

Comments
comments powered by Disqus

Navigation

Social Media