c# full basic notes and programs

  c# full basic notes and programs

 


C# Object and Class:

Since C# is an object-oriented language, program is designed using objects and

classes in C#.

C# Object

In C#, Object is a real world entity, for example, chair, car, pen, mobile, laptop

etc.

In other words, object is an entity that has state and behaviour. Here, state

means data and behaviour means functionality.

Object is a runtime entity, it is created at runtime.

Object is an instance of a class. All the members of the class can be accessed

through object.

Let's see an example to create object using new keyword.

Student s1 = new Student();//creating an object of Student

In this example, Student is the type and s1 is the reference variable that refers to

the instance of Student class. The new keyword allocates memory at runtime.

C# Class

In C#, class is a group of similar objects. It is a template from which objects are

created. It can have fields, methods, constructors etc.

Let's see an example of C# class that has two fields only.

public class Student

{

int id;//field or data member

String name;//field or data member

}

C# Object and Class Example

Let's see an example of class that has two fields: id and name. It creates instance

of the class, initializes the object and prints the object value.

using System;

public class Student

{

int id;//data member (also instance variable)

String name;//data member(also instance variable)

public static void Main(string[] args)

{

Student s1 = new Student();//creating an object of Student

s1.id = 101;

s1.name = "Sonoo Jaiswal";

Console.WriteLine(s1.id);

Console.WriteLine(s1.name);

}

}

Output:

101

Sonoo Jaiswal

C# Class Example 2: Having Main() in another class

Let's see another example of class where we are having Main() method in

another class. In such case, class must be public.

using System;

public class Student

{

public int id;

public String name;

}

class TestStudent{

public static void Main(string[] args)

{

Student s1 = new Student();

s1.id = 101;

s1.name = "Sonoo Jaiswal";

Console.WriteLine(s1.id);

Console.WriteLine(s1.name);

}

}

Output:

101

Sonoo Jaiswal

C# Class Example 3: Initialize and Display data through method

Let's see another example of C# class where we are initializing and displaying

object through method.

using System;

public class Student

{

public int id;

public String name;

public void insert(int i, String n)

{

id = i;

name = n;

}

public void display()

{

Console.WriteLine(id + " " + name);

}

}

class TestStudent{

public static void Main(string[] args)

{

Student s1 = new Student();

Student s2 = new Student();

s1.insert(101, "Ajeet");

s2.insert(102, "Tom");

s1.display();

s2.display();

}

}

Output:

101 Ajeet

102 Tom

C# Class Example 4: Store and Display Employee Information

using System;

public class Employee

{

public int id;

public String name;

public float salary;

public void insert(int i, String n,float s)

{

id = i;

name = n;

salary = s;

}

public void display()

{

Console.WriteLine(id + " " + name+" "+salary);

}

}

class TestEmployee{

public static void Main(string[] args)

{

Employee e1 = new Employee();

Employee e2 = new Employee();

e1.insert(101, "Sonoo",890000f);

e2.insert(102, "Mahesh", 490000f);

e1.display();

e2.display();

}

}

Output:

101 Sonoo 890000

102 Mahesh 490000

C# Structs

In C#, classes and structs are blueprints that are used to create instance of a

class. Structs are used for lightweight objects such as Color, Rectangle, Point

etc.

Unlike class, structs in C# are value type than reference type. It is useful if you

have data that is not intended to be modified after creation of struct.

C# Struct Example

Let's see a simple example of struct Rectangle which has two data members

width and height.

using System;

public struct Rectangle

{

public int width, height;

}

public class TestStructs

{

public static void Main()

{

Rectangle r = new Rectangle();

r.width = 4;

r.height = 5;

Console.WriteLine("Area of Rectangle is: " + (r.width * r.height));

}

}

Output:

Area of Rectangle is: 20

C# static class

The C# static class is like the normal class but it cannot be instantiated. It can

have only static members. The advantage of static class is that it provides you

guarantee that instance of static class cannot be created.

Points to remember for C# static class

C# static class contains only static members.

C# static class cannot be instantiated.

C# static class is sealed.

C# static class cannot contain instance constructors.

C# static class example

Let's see the example of static class that contains static field and static method.

using System;

public static class MyMath

{

public static float PI=3.14f;

public static int cube(int n){return n*n*n;}

}

class TestMyMath{

public static void Main(string[] args)

{

Console.WriteLine("Value of PI is: "+MyMath.PI);

Console.WriteLine("Cube of 3 is: " + MyMath.cube(3));

}

}

Output:

Value of PI is: 3.14

Cube of 3 is: 27

C# Inheritance

In C#, inheritance is a process in which one object acquires all the properties

and behaviors of its parent object automatically. In such way, you can reuse,

extend or modify the attributes and behaviors which is defined in other class.

In C#, the class which inherits the members of another class is called derived

class and the class whose members are inherited is called base class. The

derived class is the specialized class for the base class.

Advantage of C# Inheritance

Code reusability: Now you can reuse the members of your parent

class. So, there is no need to define the member again. So less code

is required in the class.

C# Single Level Inheritance Example: Inheriting Fields

When one class inherits another class, it is known as single level

inheritance. Let's see the example of single level inheritance which

inherits the fields only.

sing System;

public class Employee

{

public float salary = 40000;

}

public class Programmer: Employee

{

public float bonus = 10000;

}

class TestInheritance{

public static void Main(string[] args)

{

Programmer p1 = new Programmer();

Console.WriteLine("Salary: " + p1.salary);

Console.WriteLine("Bonus: " + p1.bonus);

}

}

Output:

Salary: 40000

Bonus: 10000

In the above example, Employee is the base class and Programmer is the

derived class.

C# Single Level Inheritance Example: Inheriting Methods

Let's see another example of inheritance in C# which inherits methods

only.

using System;

public class Animal

{

public void eat() { Console.WriteLine("Eating..."); }

}

public class Dog: Animal

{

public void bark() { Console.WriteLine("Barking..."); }

}

class TestInheritance2{

public static void Main(string[] args)

{

Dog d1 = new Dog();

d1.eat();

d1.bark();

}

}

Output:

Eating...

Barking...

C# Multi Level Inheritance Example:

When one class inherits another class which is further inherited by

another class, it is known as multi level inheritance in C#. Inheritance

is transitive so the last derived class acquires all the members of all its

base classes.

Let's see the example of multi level inheritance in C#.

using System;

public class Animal

{

public void eat() { Console.WriteLine("Eating..."); }

}

public class Dog: Animal

{

public void bark() { Console.WriteLine("Barking..."); }

}

public class BabyDog : Dog

{

public void weep() { Console.WriteLine("Weeping..."); }

}

class TestInheritance2{

public static void Main(string[] args)

{

BabyDog d1 = new BabyDog();

d1.eat();

d1.bark();

d1.weep();

}

}

Output:

Eating...

Barking...

Weeping...

Note: Multiple inheritance is not supported in C# through class.

C# Interface:

Interface in C# is a blueprint of a class. It is like abstract class because all the

methods which are declared inside the interface are abstract methods. It cannot

have method body and cannot be instantiated.

It is used to achieve multiple inheritance which can't be achieved by class. It is

used to achieve fully abstraction because it cannot have method body.

Its implementation must be provided by class or struct. The class or struct which

implements the interface, must provide the implementation of all the methods

declared inside the interface.

C# interface example

Let's see the example of interface in C# which has draw() method. Its

implementation is provided by two classes: Rectangle and Circle.

using System;

public interface Drawable

{

void draw();

}

public class Rectangle : Drawable

{

public void draw()

{

Console.WriteLine("drawing rectangle...");

}

}

public class Circle : Drawable

{

public void draw()

{

Console.WriteLine("drawing circle...");

}

}

public class TestInterface

{

public static void Main()

{

Drawable d;

d = new Rectangle();

d.draw();

d = new Circle();

d.draw();

}

}

Output:

drawing ractangle...

drawing circle... 

 

Post a Comment

Previous Post Next Post