Why we use constructor in c# | All about Constructor

What is Constructor in c#


Constructors are the special type of methods that invoked when the first object of class is created. The name of constructor is same as class name. If you do not use an access modifier with the constructor it will still be public by default.


c# class constructor example




Example:

using System;

 public class Employee   //  class
{
     public Employee()   //Constructor
     {

     }

}

Some characteristics of constructors:


  1. Constructors are always public.
  2. Constructor can be overloaded
  3. Constructors do not have any return type.
  4. Constructor is automatically declared by CLR, whenever the class is instantiated.
  5. A constructor can have any number of parameters .

Why we use constructor in c#

Constructor is used to object initialization and memory allocation of the class.
Use to initialize private fields’ value of the class whenever instance or object of class is created.

Why we can not inherit constructor

Constructors are not members, so they are not inherited by derive classes, but the constructor of the base class can be invoked from the derive class.


Behaviour of constructors in case of inheritance

The role of constructor in case of inheritance, if we create an object of derive class it first call the base class constructor then derive class constructor. It means that first the base class to be initialize then derive class. If you want to see how the base class is initialize first, may you have to put debugger and do find the step by step process by using F11 key.

C# class constructor example



using System;

public class Employee   //  class
{
    public int age;
    public string name;
    public Employee()   //Constructor
    {

    }
}
class MainClass : Employee
{
    static void Main(string[] args)
    {
        MainClass obj = new MainClass();
        obj.age = 12;
        obj.name = "Jhon";
    }
}

Put debugger and see what happen next

c# class constructor example






Read More

Asp.net web forms tutorial

  Asp.net web forms tutorial | Asp.net  tutorial with c sharp

In this tutorial I am going to tell you about asp.net web forms and how you can create your own asp.net webforms. This is the first part of asp.net webforms tutorial so,today I am talking about how to  open website in visual stdio and create webform using asp.net.
Here,in this tutorial I am using Visual Studio 2015 you can take any version of VS it's depends on you.
Allright,first of all open the visual stdio or you can use shortcut to open VS open Run Dialog box by pressing window key + R, Type devenv and press enter key. After pressing enter you can see the start screen will apper in front of you.

Step 1: 
 Click on File------->New------->Website


Asp.net web forms tutorial

Step 2: After clicking website follow next step
            Visual c#----->Asp.Net Empty Website----->Ok

Asp.net web forms tutorial

Step 3: Click on view to see the Solution Explorer (ctrl+Alt+L).


Asp.net web forms tutorial

           

Step 4: Right click on Website ---->Add--->Add new item--->Visual c#--->Webform------>ok


Asp.net web forms tutorial


I hope,first part of Asp.net web forms tutorial will be helpfull for you.


bbbbbbbb




Read More

what is the use of static class in c#.net with example

what is the use of static |static class in c#|static class with example


If class having with static keyword it becomes static class. Static class can have static function static variable and static properties. We can't create an object of static class and also we can't have sub-classes of static class.(no inheritance)

Static class contains only static member and static function if they are not static we will not able to call any thing of static class.


what is the use of static class in c#.net with example


Static classes with static data members are nothing more than global variables. All static classes are sealed (you cannot derive them from a static class).

So, the question comes in mind, if we can't create object and can't inherit then what is the use of static class.(See Below)



using System;

 public static class Employee   // Static class
{
    public static string name;
    public static int age;
    public static string work()
    {
        return "Programming";
    }
}
class MainClass
{
    static void Main(string[] args)
    {
        Employee.work();      //Able to call by the class name
        Employee.age = 21;
        Employee.name = "SOMEONE NAME";
    }
}


Why we use static class:
When we have requirement to achive something like global in our application and we don't want to create an object of class again and again (because object creating is consumption of memory) then we have to go for static class. Simply we write class name and use (.) operator, we can call the members and functions available in static class.

Note: Static class is control by CLR  and CLR provides the memory to the static class it means that use have no control over the static class.
Static class is behaves like a service or helper classes.


Read More

Inheritance in oops


Inheritance in oops:


In this tutorial you will learn  Inheritance,Purpose of Inheritance and sample code. Getting the features of one class to another class is known as inheritance.

The class which is giving features to another class is known as base class or parent class.The class which getting the features from another class is known as derived class or child class.
The actual mean of using inheritance in our application is  ‘Code reusability’ and code optimization.

Similarly in Object Oriented  Programming,Inheritance is the process of inheriting properties of objects of one class by objects of another class.

In case of inheritance if we create an object of derive class or child class we are able to call 

our own method and variable and also we can call base class methods and variable,this is the cause we say that inheritance provide me code reusability.



Inheritance in oops



For Example:


using System;


class Manager              //Parent Class or Base Class
{
    public string Mgr_name;
    int Mgr_age;
    string Mgr_address;

    public string departmant()          
    {
        return "Assistant Manager";
    }
}
class Employee:Manager    //Inheritance betweentwo classes
                         //Employee is Derive class or child class
{
    int Emp_age;
    string Emp_name;
    static void Main(string[] args)    
    {
        Employee emp = new Employee();
        emp.Mgr_name = "ManagerName";
        emp.departmant();
        //we are able to call base class variable and methods
        emp.Emp_name = "EmpName";
        emp.Emp_age = 25;
        //we are able to call own(derive class) variable and methods
    }
}




Types of inheritance in oops

1) Single inheritance
2) Multilevel inheritance
3) Hierarchical inheritance
4) Multiple inheritance
5) Hybrid inheritance

Single inheritance:
In this inheritance, there is only one base class and one derived class.

Multilevel inheritance:
In this we have many base classes and many derived classes. The classes which are acts as a base class as well as derived class known as intermediate classes.

Hierarchical inheritance:
In this, there is no. of derived classes derived from one base class.

Multiple inheritance:
In this, one class derived from multiple base classes. Multiple inheritance is not supported in c# in classes. We can achieve this by interfaces.

Hybrid inheritance:
The combination of any two inheritances is known as hybrid inheritance.


Also Read:


Call javascript function in body onload event 




Read More