C# .Net methods and properties

Any class in an object-oriented language has method and property members. These are the places where the actual business logic or functionality is written and executed. This tutorial explains how to create and use methods and properties in C#.

C# Methods:

Method is object-oriented item of any language. All C# programs are constructed from a number of classes and almost all the classes will contain methods. A class when instantiated is called an object. Object-oriented concepts of programming say that the data members of each object represent its state and methods represent the object behavior.

Method Signature in C#:

Each method is declared as follows:

    Return-type methodname ( Parameterslist );

For better understanding of methods let consider following example. We have a class Man. It can have many fields like that:

public class Man
{
public Man(){}
private int m_old;
private string m_name;
public string WhatIsYourName()
{
Console.WriteLine(m_name);
return m_name;
}
public string HowOldAreYou()
{
Console.WriteLine(m_old.ToString());
return m_old;
}
}

The private members m_old and m_name define some state of objects that can be created as instances of our class. Also the class Man has two methods, which serve some of our requests. Method string WhatIsYourName() writes current object?s name to the console and returns it, and the second one similar to first return age of man and also writes an output to the console.

The return type in the example above returns strings, which is an in-built data type. The methods can also return any generic C# type or any custom types created by us.

Passing Parameters to Methods in C#:

The input parameters can be passed in two ways.

  • Value type
  • Reference type.

If parameters are passed as value types a new copy of it will be created and passed inside the function. If they are created as reference types, only the address of the parameters will be passed.

See next example:

public int CalculateBirthYear(int year)
{
int b = year - m_old;
Console.WriteLine("Birth year is {0}",b);
return b;
}

If input parameter pass as reference type it must use keyword ref, in that way we operate with the same cell in memory. That?s mean it can be changed inside any method. A small example for a parameter passed by reference is:

public int CalculateBirthYear(ref int year)
{
int b = year - m_old;
Console.WriteLine("Birth year is {0}",b);
return b;
}

Now, the function CalculateBirthYear can even modify the value of year as it is passed by reference.

 

Output Parameters in Methods:

The return values in any function will be enough for any one if only one value is needed. But in case a function is required to return more than one value, then output parameters are the norm. This is not supported in C++ though it can be achieved by using some programming tricks. In C# the output parameter is declared with the keyword out before the data type. A typical example is as follows.

public void CalculateBirthYear(ref int year, out int birthyear)
{
int b = year - m_old;
Console.WriteLine("Birth year is {0}",b);
birthyear = b;
return;
}

Strictly speaking there is no difference between ref and out parameters. The only difference is that the ref input parameters need an input value and the out parameters don?t.

Variable arguments in C#:

The C# language supports variable arguments through a keyword called params. A typical example for the declaration of a function with variable argument signature is as follows.

Public void functionName(int a, params int[] varParam);

Method Overloading in C#:

A method is considered to be an overloaded method, if it has two or more signatures for the same method name. These methods will contain different parameters but the same return types.

A simple example for an overloaded methods are:

Public void functionName(int a, params int[] varParam);
Public void functionName(int a);

 

Property in C#:

Property ? it is a special method that can return a current object?s state or set it. Simple syntax of properties can see in the following example:

public int Old
{
get {return m_old;}
set {m_old = value;}
}
public string Name
{
get {return m_name;}
}

Here are two types of properties. A first one can set or get field of class named m_old, and the second is read only. That?s mean it can only get current object?s state.

The significance of these properties is its usability. These properties need not be called with any function names like objectname.get or objectname.set etc., But they can be directly assigned the values or retrieve the values.

A usage of method and properties you can see in attached example. It can be compiled using MS Visual Studio command line. Do get an executable file must do the next steps: In Start menu of Windows should find Programs->MS Visual Studio .NET->MS Visual Studio .NET Tools-> Visual Studio .NET Command Prompt. So run it, and type csc. It is command that run csharp compiler, after it, should type a lines similar to it: csc /out: My.exe My.cs. Now we can run our program from exe file.

And at least few words about access modifiers of methods and properties. In examples there?re only public, but they can also be declared as private, protected or internal. Also aditional modifiers are new, static, virtual, abstract, override. All these will be dealt in next tutorials.Download the sample code from here.