Electronic and Electric Circuit Simulation
Home C Sharp Code Examples c sharp class example constructor
c sharp class example constructor PDF Print E-mail

 

C Sharp is object oriented programming language. It means that programmers should be object oriented in thinking while writing down code in C#. For simplicity, and completeness of this tutorial, our first c sharp class example will be given as part of Console Application, that is also C# class.

 

 

 

 

 

 

 

 

Scope of this example is focus on c sharp class constructor as well as on c sharp class method. Class is simple and it is arithmetic class that has two integer numbers with method for adding this two numbers and method for subtracting this two numbers.

Firstly, class should be declared:

public class arithm
{
}

Inside, class members should be declared:

int int_member1, int_member2;

Now, it is time to make at least one (there can be many with different number of arguments) c sharp class constructor. Class constructor(s) must have same name as class itself.

public arithm (int s1, int s2)
{

int_member1 = s1;
int_member2 = s2;

}

Since c sharp class method is there to do some data processing over class members, it is practical to have class methods that take no arguments and that returning the result.


public int sum()
{

return int_member1 + int_member2;

}
public int difference()
{

return int_member1 - int_member2;

}

Defined class can be used in main program by making corresponding object:

arithm arit = new arithm(21, 9);
Console.WriteLine("Sum is:" + arit.sum() + " and difference is " + arit.difference() );

Whole code for given example is:

public class arithm
{

int int_member1, int_member2;
public arithm(int s1, int s2)
{

int_member1 = s1;
int_member2 = s2;

}
public int sum()
{

return int_member1 + int_member2;

}

public int difference()
{

return int_member1 - int_member2;

}

}


static void Main(string[] args)
{
arithm arit = new arithm(21, 9);
Console.WriteLine("Sum is:" + arit.sum() + " and difference is " + arit.difference() );
}

}

There is also alternative way for setting class members without constructor (with additional class methods for accessing class data members), but it is less practical:

public void setmember1(int s)
{

int_member1 = s;

}
public void setmember2 (int s)
{

int_member2 = s;

}

And handling object in main program would go like this:

arithm arit = new arithm(21, 9);
Console.WriteLine("Sum is:" + arit.sum() + " and difference is " + arit.difference() );
Console.WriteLine("------------------");
arit.setmember1(57);
arit.setmember2(54);
Console.WriteLine("Sum is:" + arit.sum() + " and difference is " + arit.difference());

Next reading >>

External links:

C Sharp Class Example on dotnetperls
C Sharp Class Example on microsoft
C Sharp Class Example on tutorialspoint

 
Copyright © 2014 Electronic and Electric Circuit Simulation. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License. Top joomla 1.5 themes
Top joomla 1.5 themes designed by Lonex.

Recent/Mostly Visited

Simulate recent1: Instrumentation Amplifier

Simulate recent2:Forward Converter

Simulate recent3:Sawtooth Generator