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.
For better understanding of methods let consider following example. We have a class
Man. It can have many fields like that:
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.
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:
Now, the function CalculateBirthYear can even modify the value of year as it is passed by
reference.
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.
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:
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.