Methods In C#.NET

by


Methods are very useful in C#. Since any code is to be used at many places, we can define that in a method and can use the code only by calling that method .

Method signature

A method signature contains various things, which are stated below.

[Attributes]    (e.g.: [WebMethod],[HttpPost],[HttpGet])Public void TestMethod(string parameters)


   Explanation

       In the method shown above, Public signature defines the Access modifier.

       Void defines the no return type (in case of void, No return type is there and In case of string, its string) of the method.

       If the return type is defined as string, then the method must return some string as its output. If we do not want a method to return anything, we can define it as void.

TestMethod

It defines the name of that method.

The string parameters defines the parameter that this method will expect during a call. 

 

 class Program      {  public static void Main(string[] args)          {    InstanceMethodClass  p = new InstanceMethodClass();              p.InstanceMethod();  }    }    public class InstanceMethodClass      {  //This is an Instance method.  public void InstanceMethod()          {              Console.WriteLine("This is a test instance method.");          }  } 

Static Method

We do not need to create the object of the class in which method is defined. We can invoke the method by using it directly.

class Program      {          public static void Main(string[] args)          {              ClassForStaticMethod.StaticMethod();          }  }     public class ClassForStaticMethod      {             public static void StaticMethod()          {               Console.WriteLine("This is a test static method.");          }      }  

Let's take an example of method with the parameters.

In the example shown above, we need to pass a string argument because "PrintParameter" method expects the string as parameter.

Let's take an example of return type method.

class Program      {          public static void Main(string[] args)          {  InstanceMethodClass i=new InstanceMethodClass();  i.PrintParameter("Argument passed.");  }  }   public class InstanceMethodClass{      public void PrintParameter(string parameter)          {              Console.WriteLine(parameter);          }  }  

In the example shown above, we need to pass a string argument because "PrintParameter" method expects the string as parameter.

Let's take an example of return type method.

class Program      {          public static void Main(string[] args)          {  InstanceMethodClass i=InstanceMethodClass();   int sum=i.ReturnSum(2,6);              Console.WriteLine(sum);    }  }     public class InstanceMethodClass      {  public  int ReturnSum(int a,int b)          {              int sum = a + b;              return sum;          }  }  

In the example shown above, we used int as return type instead of void. Thus, the method must return an integer. When we invoke this method, we can get the returned result in an Int variable.

In the two examples stated above, it will remain the same for static method but the only difference is the method needs to be defined as static and invoked without using a class instance. 

Thank you.

Source c-sharpcorner , MSDN

Trim String Method in C#

by

Triming String in C# , some times we need it for white speaces or

cut some characters from our text.

there is 3 methods :

Trim()

TrimEnd()

TrimStart()

Removes trailing punctuation and white-space characters from the current String object.

 

 

Examples

The following example uses the TrimStart method to trim white space.

string str="ABC  ";

string trimedStr=str.TrimEnd();

Result will be : "ABC".

Same we can use TrimStart() method to trim and characters  we dont need .

one more Example :

I we need to trim two characters in the same time :

string lines="// This code displays a simple greetingsring newLine=line.TrimStart(' ', '/');

 

Example with Array:

 

string[] items = new string[] { "MyBlog?", "Hi..." };

// Loop and call TrimEnd.

foreach (string item in items)

{ string trimmed = item.TrimEnd('?', '.');    //Here will remove (?  and . ) 

}

 

Result :

MyBlog

Hi

 

I wish this simple code is useful