Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Generics

Generics

Scheduled Pinned Locked Moved C#
questionhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Satish Pai
    wrote on last edited by
    #1

    Hi Friends I would i like question u all abt Generics. I have read abt Generics but there is a doubt & i would i like to question u using simple program. public class Student<T> { private string name = "satish"; private Int16 RollNo = 12; public void displayStudent() { Console.WriteLine(name); Console.WriteLine(RollNo); Console.ReadLine(); } } class Program { static void Main(string[] args) { Student<int> s1 = new Student<int>(); s1.displayStudent(); } } If i am right then the above Student class is called Generic class. What is the use of creating a class like this if we are using a method & we are just specifying the Type in angle bracket. so Please help me :)

    D 1 Reply Last reply
    0
    • S Satish Pai

      Hi Friends I would i like question u all abt Generics. I have read abt Generics but there is a doubt & i would i like to question u using simple program. public class Student<T> { private string name = "satish"; private Int16 RollNo = 12; public void displayStudent() { Console.WriteLine(name); Console.WriteLine(RollNo); Console.ReadLine(); } } class Program { static void Main(string[] args) { Student<int> s1 = new Student<int>(); s1.displayStudent(); } } If i am right then the above Student class is called Generic class. What is the use of creating a class like this if we are using a method & we are just specifying the Type in angle bracket. so Please help me :)

      D Offline
      D Offline
      Dan Mos
      wrote on last edited by
      #2

      Hy, This MSDN usage definition on Generic Types is the best I think:

      • You can use generic types/classes to maximize code reuse, type safety, and performance.
      • The most common use of generics is to create collection classes.

      List<T> is possibly one of the best example: It has generic type in that you can create a List of ints, strings... your own defined types. It has type safety => you can not add "Dan" to a list if ints It has performance => no need to cast from object to yourType [EDIT]Here's a better usage of generics for your example:

      public class Student
      {
      private string name = "satish";
      private Int16 RollNo = 12;

      public void displayStudent()
      {
      Console.WriteLine(name);
      Console.WriteLine(RollNo);
      Console.ReadLine();
      }
      }

      class Program
      {
      static void Main(string[] args)
      {
      List<Student> students = new List<Student>();
      students.Add(new Student());
      foreach(var std in students){
      std.displayStudents();
      }
      }
      }

      [/EDIT]

      modified on Monday, February 22, 2010 1:12 AM

      S 1 Reply Last reply
      0
      • D Dan Mos

        Hy, This MSDN usage definition on Generic Types is the best I think:

        • You can use generic types/classes to maximize code reuse, type safety, and performance.
        • The most common use of generics is to create collection classes.

        List<T> is possibly one of the best example: It has generic type in that you can create a List of ints, strings... your own defined types. It has type safety => you can not add "Dan" to a list if ints It has performance => no need to cast from object to yourType [EDIT]Here's a better usage of generics for your example:

        public class Student
        {
        private string name = "satish";
        private Int16 RollNo = 12;

        public void displayStudent()
        {
        Console.WriteLine(name);
        Console.WriteLine(RollNo);
        Console.ReadLine();
        }
        }

        class Program
        {
        static void Main(string[] args)
        {
        List<Student> students = new List<Student>();
        students.Add(new Student());
        foreach(var std in students){
        std.displayStudents();
        }
        }
        }

        [/EDIT]

        modified on Monday, February 22, 2010 1:12 AM

        S Offline
        S Offline
        Satish Pai
        wrote on last edited by
        #3

        hi sir, Thank u that u replied soon sir But what happens in Memory. List is of Object type & if i specify Int is of Value type. Then what exactly happens ? Thank u once again

        D 1 Reply Last reply
        0
        • S Satish Pai

          hi sir, Thank u that u replied soon sir But what happens in Memory. List is of Object type & if i specify Int is of Value type. Then what exactly happens ? Thank u once again

          D Offline
          D Offline
          Dan Mos
          wrote on last edited by
          #4

          Hy, you did it all wrong. look at my modified version.

          S 1 Reply Last reply
          0
          • D Dan Mos

            Hy, you did it all wrong. look at my modified version.

            S Offline
            S Offline
            Satish Pai
            wrote on last edited by
            #5

            ya i saw your Program sir. now i understood thank u very much :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups