Wednesday, 9 May 2018

Default and parameterized Constructor in C#





main class :

using ConsoleApplication11.Model;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication11

{

    class Program

    {

        static void Main(string[] args)

        {

            Employee emp = new Employee();

            emp.id = 1;

            emp.name = "ali";

            emp.salary = 50000;

            emp.showdata();

            Employee emp1 = new Employee(2, "ahmed", 60000);



            emp1.showdata();

            

            





        }

    }



}


Employee class: 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication11.Model
{
    class Employee
    {
        public int id;
        public string name;
        public float salary;
        public Employee() //default constructor..............
        {
            Console.WriteLine("Default Constructor is called");
        }

        public Employee(int id,string name,float salary) //default constructor..............
        {
            this.id = id;
            this.name = name;
            this.salary = salary;
            Console.WriteLine("Parametrized Constructor is called");
        }


        public void showdata()
        {
            Console.ForegroundColor = System.ConsoleColor.Cyan;
            Console.BackgroundColor = System.ConsoleColor.Red;
            Console.WriteLine("ID="+id);
            Console.WriteLine("Name = "+name );
            Console.WriteLine("Salary= "+salary);

        }

    }
}

No comments:

Post a Comment

Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...