C# class

class is a data structure containing different data types, methods, and subclasses.

class A
{
public int i;
public string str;
public A() { i = 2; }
public string irate(double d)
{
double d2 = i * (1 + d);
return d2.ToString();
}
}
A obj = new A();

Class may have methods with the same name but different parameter numbers or types, which is overriding.
class A
{
public int i;
public string str;
public A() { i = 2; }
public string irate(double d)
{
double d2 = i * (1 + d);
return d2.ToString();
}
public string irate(string s) //overrided method irate
{
return s;
}
}

Class inheritance.
class A
{
public int i;
public string str;
public A() { i = 2; }
public string irate(double d)
{
double d2 = i * (1 + d);
return d2.ToString();
}
public string irate(string s) //overrided method irate
{
return s;
}
}
class A2:A
{
double sum;
public A2() { sum = 4; }
public double sumup()
{
return sum=sum + this.i;
}
}
A2 obj = new A2();
double ori = obj.sumup(); //ori = 6;

A class with Generics can handle different data types.
class tt<T>
{
public string add(T a,T b)
{
string c= a.ToString() + " " + b.ToString();
return c;
}
}
tt<int> t = new tt<int>();
string ret = t.add(3,4); //3 4
tt<double> t2 = new tt<double>();
string ret2 = t2.add(3,4.54); //3 4.54
tt<string> t3 = new tt<string>();
string ret3 = t3.add("orange","good"); //orange good



endmemo.com © 2024  | Terms of Use | Privacy | Home