Thursday 24 May 2018

List T in C# (GENERIC CLASSES)







class Employee

    {



        public int id { get; set; }

        public string name { get; set; }



    }





--------------------------------------------------------------------------



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication30

{

    class Program

    {

        static void Main(string[] args)

        {

            List<Employee> li = new List<Employee>();

            Console.WriteLine("How many records you want to add in list:");

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

            for (int i = 0; i < n; i++)

            {

                Console.ForegroundColor = System.ConsoleColor.Yellow;

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

                Employee emp = new Employee();

                emp.name = Console.ReadLine();

                emp.id = i + 1;

                li.Add(emp);

                Console.ForegroundColor = System.ConsoleColor.Green;

                Console.WriteLine("1 RECORDS SUCCESSFULLY INSERTED IN LIST.......");

                Console.WriteLine("PRESS ANY KEY.....");

                Console.ReadLine();

                Console.Clear();



            }



            Console.WriteLine("PRESS ANY KEY.....");

            Console.ReadLine();

         

            foreach (var item in li)

            {

                Console.ForegroundColor = System.ConsoleColor.Red;

                Console.WriteLine("-----------------------------------------------");

                Console.ForegroundColor = System.ConsoleColor.Cyan;

                Console.WriteLine("ID: "+item.id);

                Console.WriteLine("Name: " + item.name);

                Console.ForegroundColor = System.ConsoleColor.Red;

                Console.WriteLine("-----------------------------------------------");

               



            }



            Console.ReadLine();

        }

    }

}


Collection Classes in C# Generic Classes







using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication29

{

    class Program

    {

        static void Main(string[] args)

        {

            string t;

            List<string> li = new List<string>();

            Console.WriteLine("How many names you want to add in list: ");

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

            for (int i = 0; i < n; i++)

            {

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

                t = Console.ReadLine();

                li.Add(t);

                Console.ForegroundColor = System.ConsoleColor.Green;

                Console.WriteLine("Data successfully added in list.....");

                Console.ReadLine();

                Console.Clear();

                Console.ForegroundColor = System.ConsoleColor.White;

           

            }

            Console.ForegroundColor = System.ConsoleColor.Green;

           

            Console.WriteLine("Press any key to print all names");

            Console.ReadLine();



            foreach (var item in li)

            {

                Console.ForegroundColor = System.ConsoleColor.Yellow;

                Console.WriteLine("name: "+item);

            }



            Console.ReadLine();

        }

    }

}


Serialization and Deserialization in C#





C# Serialization

string path = @"C:\Users\salman\Desktop\FILE\serial.doc";

         

            Student s = new Student(1, "Salman");

            FileStream stream = new FileStream(path, FileMode.OpenOrCreate);

            BinaryFormatter formater = new BinaryFormatter();

            formater.Serialize(stream, s);

            stream.Close();

            Console.ForegroundColor = System.ConsoleColor.Red;

            Console.WriteLine("File saved in "+path);

            Console.ReadLine();



------------------------------------------------------------------------------

C# Deserialization





string path = @"C:\Users\salman\Desktop\FILE\serial.doc";

 FileStream stream = new FileStream(path, FileMode.OpenOrCreate);

            BinaryFormatter formater = new BinaryFormatter();

            Student s = (Student)formater.Deserialize(stream);

            Console.ForegroundColor = System.ConsoleColor.Green;

            Console.WriteLine("Id :"+s.id);

            Console.WriteLine("Name :" + s.name);

            Console.ReadLine();
--------------------------------------------------------------

STUDENT CLASS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication28
{   [Serializable]
    class Student
    {
        public int id;
        public string name;

        public Student(int id, string name)
        {
            this.id = id;
            this.name = name;

        }

    }
}

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