Class in C#

Class in C#

Let’s write a simple class in C#:

/* A simple class for adding two integers */

using System;

class Moro
{
public int a;
public int b;
}

class Poro
{
public static void Main()
{
int c;
int d;

Moro pik = new Moro();
pik.a = 2;
pik.b = 3;
c = pik.a + pik.b;

Moro lik = new Moro();
lik.a = 5;
lik.b = 6;
d = lik.a + lik.b;

Console.WriteLine(“pik: ” + c);
Console.WriteLine(“lik: ” + d);
}
}

We have the result:

D:\SharpDevelop Projects\CSharp>csc cs9.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.

D:\SharpDevelop Projects\CSharp>cs9
pik: 5
lik: 11

D:\SharpDevelop Projects\CSharp>

So it’s work! Let’s add a method:

/* A simple class for adding two integers */

using System;

class Moro
{
public int a;
public int b;
public double wynik;

public double Mira()
{
wynik = a + b * 100;
return wynik;
}
}

class Poro
{
public static void Main()
{
int c;
int d;
double f;
double g;

Moro pik = new Moro();
pik.a = 2;
pik.b = 3;
c = pik.a + pik.b;
f = pik.Mira();

Moro lik = new Moro();
lik.a = 5;
lik.b = 6;
d = lik.a + lik.b;
g = lik.Mira();

Console.WriteLine(“pik: ” + c + ” and Mira: ” + f);
Console.WriteLine(“lik: ” + d + ” and Mira: ” + g);
}
}

We have the result:

D:\SharpDevelop Projects\CSharp>csc cs10.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.

D:\SharpDevelop Projects\CSharp>cs10
pik: 5 and Mira: 302
lik: 11 and Mira: 605

D:\SharpDevelop Projects\CSharp>

Now, let’s write a class with constructor:

using System;
class Boro
{
public int an;
public int bn;
public int wynik;

public Boro(int a, int b)
{
{
an = a;
bn = b;
}
}

public int Goga()
{
{

wynik = an + bn;
return wynik;
}
}
}

class Moka
{
public static void Main()
{
int c;
int d;
int e;
int f;

Boro adel = new Boro(1,2);
c = adel.Goga();
Console.WriteLine(“Mamy ” + c);

Boro adel1 = new Boro(3,8);
d = adel1.Goga();
Console.WriteLine(“Mamy ” + d);

Boro adel2 = new Boro(12345,5566);
e = adel2.Goga();
Console.WriteLine(“Mamy ” + e);

Boro adel3 = new Boro(-123,455);
f = adel3.Goga();
Console.WriteLine(“Mamy ” + f);

}
}

We have the result:

D:\SharpDevelop Projects\CSharp>cs12
Mamy 3
Mamy 11
Mamy 17911
Mamy 332

D:\SharpDevelop Projects\CSharp>

We can have a method overloading if we need:

using System;
class Mat
{
public int Fig(int a)
{
return a * a ;
}
public double Fig(double a)
{
return a * a ;
}
public int Fig(int a, int b)
{
return a * b;
}
public double Fig(double a, double b)
{
return a * b;
}
}

class Obl
{
public static void Main()
{
Mat ob = new Mat();
Console.WriteLine(“Suma: ” + ob.Fig(2));

Mat ob1 = new Mat();
Console.WriteLine(“Suma: ” + ob1.Fig(0.3));

Mat ob2 = new Mat();
Console.WriteLine(“Suma: ” + ob2.Fig(4,5));

Mat ob3 = new Mat();
Console.WriteLine(“Suma: ” + ob3.Fig(4.5, 6.7));
}
}

We have the result:

D:\SharpDevelop Projects\CSharp>cs13
Suma: 4
Suma: 0.09
Suma: 20
Suma: 30.15

D:\SharpDevelop Projects\CSharp>

Now,  we can see how method overloading works for constructors:

using System;

class Spok
{
public int an;
public int bn;
public double cn;
public double dn;
public double en;

public Spok (int a, int b, double c)
{
an = a;
bn = b;
cn = c;
}

public Spok (int a, int b)
{
an = a;
bn = b;
}

public Spok(double d, double e)
{
dn = d;
en = e;
}

public double Oro()
{
return an + bn + cn + dn + en;
}
}

class Eti
{
public static void Main()
{

Spok adel = new Spok(1,2);
Console.WriteLine(adel.Oro());
Spok adel1 = new Spok(1,2,3);
Console.WriteLine(adel1.Oro());
Spok adel2 = new Spok(1.2, 4.5);
Console.WriteLine(adel2.Oro());
}
}

We have the result:

D:\SharpDevelop Projects\CSharp>cs14
3
6
5.7

D:\SharpDevelop Projects\CSharp>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s