class Program
{
static void Main(string[] args)
{
Console.WriteLine("=> Rectangular multidimensional array.");
// A rectangular MD array.
int[,] myMatrix;
Random r = new Random();
myMatrix = new int[6, 6];
// Populate (6 * 6) array.
for (int i = 0; i < 6; i++)
for (int j = 0; j < 6; j++)
myMatrix[i, j] = r.Next(100);
// Print (6 * 6) array.
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
Console.Write(myMatrix[i, j] + "\t");
Console.WriteLine();
}
Console.ReadLine();
}
}
2. Introduction to C# method
- Pass by Reference , Output and params Parameter in Method
- Method Overloading in C
// Valiables
int k = 0;
int k = 0;
int a = 10;
int b = 20;
int c = 0, d = 0;
string[] myArray = new string[4];
myArray[0] = "Azaa";
myArray[1] = "Bataa";
myArray[2] = "Zoloo";
myArray[3] = "Azaa";
// method 1
saam(k);
Console.WriteLine(k);
// method 2
sum(ref a, ref b);
Console.WriteLine(a + " " + b);
// method 3
method2(a, b, out c, out d);
Console.WriteLine("Sum is : {0} and Prod is : {1}", c, d);
// method 4
Amethod();
Amethod("n1", "n2", "n3");
Amethod(myArray);
Console.ReadKey();
}
public static void saam(int x)
{
x = 10;
}
public static void sum(ref int x, ref int y)
{
int c = x;
x = y;
y = c;
}
public static void method2(int a, int b, out int sum1, out int sum2)
{
sum1 = a + b;
sum2 = a * b;
}
public static void Amethod(params string[] names)
{
foreach (string a in names)
Console.WriteLine(a);
Console.WriteLine("Array Size = {0}", names.Length);
}
3. Polymorphism in C#
Example 1:
class Box{
public virtual void Display()
{
Console.WriteLine("Box Class");
}
}
class Circle : Box
{
public override void Display()
{
//base.Display();
Console.WriteLine("Circle Class");
}
}
class Line : Box
{
public override void Display()
{
//base.Display();
Console.WriteLine("Line Class");
}
}
class Point : Box
{
public new void Display()
{
//base.Display();
Console.WriteLine("Point Class");
}
}
class Program
{
static void Main(string[] args)
{
Box[] b = new Box[3];
b[0] = new Box();
b[1] = new Circle();
b[2] = new Line();
foreach (Box a in b) a.Display();
Box c = new Point();
c.Display();
Console.ReadKey();
}
}
4. Properties / getter, setter /
class Box{
private int id;
private string author;
public int ID { get; set; }
public string Author { get; set; }
}
class Program
{
static void Main(string[] args)
{
Box b = new Box();
b.ID = 11;
b.Author = "Zoloo";
Console.WriteLine("book id is {0}, author is {1}", b.ID, b.Author);
Console.ReadKey();
}
}
5. Abstract Class and Interface in C#
3. Polymorphism in C#
Example 1:
class Box{
public virtual void Display()
{
Console.WriteLine("Box Class");
}
}
class Circle : Box
{
public override void Display()
{
//base.Display();
Console.WriteLine("Circle Class");
}
}
class Line : Box
{
public override void Display()
{
//base.Display();
Console.WriteLine("Line Class");
}
}
class Point : Box
{
public new void Display()
{
//base.Display();
Console.WriteLine("Point Class");
}
}
class Program
{
static void Main(string[] args)
{
Box[] b = new Box[3];
b[0] = new Box();
b[1] = new Circle();
b[2] = new Line();
foreach (Box a in b) a.Display();
Box c = new Point();
c.Display();
Console.ReadKey();
}
}
Polymorphism
Example 2:
Animal
class:
class Animal
{
string type;
string color;
string weight;
string height;
int age;
int numofLegs;
public int Age{
get { return this.age; }
set { this.age = value; }
}
public virtual void move()
{
Console.WriteLine("Animal
Moved!");
}
public void makenoise()
{
Console.WriteLine("Animal Made
noise!");
}
}
Dog
Class:
class Dog : Animal
{
string name;
string owner;
public override void move()
{
Console.WriteLine("Dog
moved!");
}
}
Main
Program:
class Program
{
static void Main(string[] args)
{
Dog d = new Dog();
d.Age = 13;
Console.WriteLine(d.Age);
d.move();
d.makenoise();
Console.ReadLine();
}
}
class Box{
private int id;
private string author;
public int ID { get; set; }
public string Author { get; set; }
}
class Program
{
static void Main(string[] args)
{
Box b = new Box();
b.ID = 11;
b.Author = "Zoloo";
Console.WriteLine("book id is {0}, author is {1}", b.ID, b.Author);
Console.ReadKey();
}
}
5. Abstract Class and Interface in C#
Interface example:
namespace Tutorial2
{
interface IMeter1
{
void print1();
}
interface IMeter2
{
void print2();
}
class Meter : IMeter1, IMeter2
{
public void print()
{
Console.WriteLine("Hello
Class");
}
void IMeter1.print1()
{
Console.WriteLine("Hello
Interface 1");
}
void IMeter2.print2()
{
Console.WriteLine("Hello Interface
2");
}
}
class Program
{
static void Main(string[] args)
{
IMeter1 met1 = new Meter();
IMeter2 met2 = new Meter();
Meter m = new Meter();
m.print();
met1.print1();
met2.print2();
Console.ReadKey();
}
}
}
Abstract
class:
namespace Tutorial2
{
abstract class Meter
{
public int a;
public abstract void printer();
public void printer2()
{
Console.WriteLine("Abstract
printer 2");
}
}
class Program : Meter
{
public override void printer()
{
Console.WriteLine("Abstract
printer 1");
}
static void Main(string[] args)
{
Program p = new Program();
p.printer();
p.printer2();
p.a = 12;
Console.ReadKey();
}
}
}
6. Delegates in C#
Simple Delegate Example
namespace Tutorial2
{
public delegate int addNumdelegate(int n1, int n2);
class Program
{
public static int addNum(int n1, int n2)
{
return n1 + n2;
}
public static int subNum(int n1, int n2)
{
return n1 - n2;
}
static void Main(string[] args)
{
addNumdelegate ad1 = new addNumdelegate(addNum);
int r = ad1(15, 16);
addNumdelegate ad2 = new addNumdelegate(subNum);
int r2 = ad2(18, 15);
Console.WriteLine(r);
Console.WriteLine(r2);
Console.ReadKey();
}
}
}
хэрэг болох линкийг оруулав: https://www.youtube.com/watch?v=Lf49f_-ZqX8
No comments:
Post a Comment