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

Features of .NET Framework in c#



NET framework provides a user friendly environment for developers to develop, run and deploy the following applications.
NET framework provides a consistent programming languauges to perform several tasks like connecting and retrieving databases, and reading from and writing files.





.NET framework class Library:

The .NET framework class library contains predefined classes which are useful for developers to develop code in applications. Microsoft has created these classes to develop the various applications. This library contains several namespaces and assemblies also.

Common Language Runtime (CLR):

The CLR creates the interoperability between different .NET languages, such as C#, visual basic, by providing common environment for the execution of code written any of these languages.
Every language in .NET has its own compiler, after compiling their codes in their compilers, MSIL code will be generated. This MSIL code is known as intermediate code. This intermediate code is common for all languages in .NET framework. So, CLR provides environment to execute intermediate code.
CLR Responsibilities:         
  • Garbage collection: CLR automatically clean up resources during the execution of the programs.
  • Jit compilation: CLR loads MSIL codes on jit-compilers for execute the code.
  • Code Access Security: in this, CLR provides security objects to access code during execution.
  • Automatic memory management: it has built in functions for allocate or de-allocate the memory of .NET objects.
  • Code verification: It is type safety and prevents code from illegal operations.


Common Type System (CTS):

Common type system provides support for multiple languages because it contains a type system that is common for all languages.CTS provides common data type system for all .net languages. It provides common type system that will use in MSIL code.
For example,
In C#, we have data type “int”.
In VB, we have data type for integer values “integer”.
But CTS uses the data type “system.int32” as a common data type for integer values. This data type is used for ‘int’ and ‘integer’ in MSIL code.

Common Language Specification (CLS):

Common language Specification is a set of rules, which provides interoperability between two .net languages.
CLS is a subset of CTS.
CTS provides rules to design application programming interfaces (API) which are used by all .net languages.




Read More

Bind images in DataList from folder in ASP.Net using C#


I will explain how to save images into folder and how we can bind that images
to datalist using asp.net. It's very easy to store images in folder and binding
that images to datalist. This topic shows how to integrate BinaryImage control into  a DataList control to display images stored as binary images, in data binding scenarios. 








Step 1:

CREATE TABLE [products]
(
            [ID] [numeric](18, 0) NOT NULL,
            [ProductName] [nvarchar](50) NULL,     
            [Category] [varchar](50) NULL,
            [Price] [int] NULL,
            [Image] [varchar](50) NULL,
)


Step 2:

SELECT  [ProductName], [Image], [Price],[Category] from products

ProductName           Image                                      Price              Category
LG                          ~\images\lg\lg.jpg                  10000             LG
Nexus 5                ~\images\lg\nexus.jpg           25000              LG
Optimus              ~\images\lg\optim.jpg          30000              LG
Flex                       ~\images\lg\fl.jpg                   20000              LG
GW-620                ~\images\lg\gw 620.jpg         8000                LG
Verizon               ~\images\lg\ver.jpg                12000              LG

SELECT [ProductName], [Image], [Price] FROM [products] WHERE ([Category]='LG')

ProductName                   Image                              Price
LG                                ~\images\lg\lg.jpg           10000
Nexus 5                    ~\images\lg\nexus.jpg       25000
Optimus                  ~\images\lg\optim.jpg      30000
Flex                               ~\images\lg\fl.jpg           20000
GW-620                      ~\images\lg\gw 620.jpg   8000
Verizon                         ~\images\lg\ver.jpg      12000





Under Div section you have to place this code



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
<div class="center_content">
            <div class="center_title_bar">My Product</div>
    <br /><br />  
    <br />
     <asp:DataList ID="DataList5" runat="server" CellPadding="6" CellSpacing="6"
         DataSourceID="SqlDataSource5" RepeatColumns="3" Width="550px">
         <ItemTemplate>
        <div class="center_prod_box">
                 <table class="prod_box">     
                    <tr>
                        <td class="product_title">
                            <asp:HyperLink  ID = "HyperLink1" runat="server" Text='<%# Eval("ProductName") %>' Font-Underline="False" NavigateUrl='<%#"product details.aspx?ProductName="+Eval("ProductName") %>'></asp:HyperLink>
                        </td>
                    </tr>
                    <tr>                    
                        <td class="product_img">
                            <asp:ImageButton ID="ImageButton1" runat="server" Height="90px" ImageUrl ='<%# Eval("Image") %>' ImageAlign = "AbsMiddle" PostBackUrl='<%#"product details.aspx?ProductName="+Eval("ProductName") %>'/>                                                 
                        </td>
                    </tr>
                    <tr>
                    <td class="prod_price">
                        <asp:Label ID="Label1" runat="server" Text = '<%# Eval("Price") %>' ></asp:Label>
                    </td>
                    </tr>
                </table>
                </div>
    </ItemTemplate>
     </asp:DataList>
     <br />
     <asp:SqlDataSource ID="SqlDataSource5" runat="server"
         ConnectionString="<%$ ConnectionStrings:shauryaConnectionString %>"
         SelectCommand="SELECT [ProductName], [Image], [Price] FROM [products] WHERE ([Category]='LG')">
     </asp:SqlDataSource>
     </div>
</form>
</body>
</html>



Note:In above Source code I m using my own css for designing,may be U.I. will look like different in your case.





Read More