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);

        }

    }
}

Class and object in C# with real world Example





main class



using ConsoleApplication10.Model;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication10

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("How many Student you want to Add in Record: ");

            int n = int.Parse(Console.ReadLine());

            Student[] s = new Student[n];

            int id;

            string name;

            for (int i = 0; i < s.Length; i++)

            {

                id = i + 1;

                Console.WriteLine("Enter the name of student: ");

                name = Console.ReadLine();

                s[i] = new Student();

                s[i].insert(name, id);





            }



            foreach (Student item in s)

            {

                item.showRecord();

            }



            Console.ReadLine();

           

        }

    }

}

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

namespace ConsoleApplication10.Model
{
    class Student
    {
        public int id;
        public string name;
        public  Student()
        {

        }
        public void insert(string n,int id)
        {
            this.id = id;
            this.name = n;
        }
        public void showRecord()
        {
            Console.ForegroundColor = System.ConsoleColor.Green;
            Console.WriteLine("id : " + id);
            Console.WriteLine("Name : "+name);
            Console.ForegroundColor = System.ConsoleColor.White;

        }

    }
}

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 ...