Wednesday, 9 May 2018

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;

        }

    }
}

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