Question about C # Generators

I am trying to finish a practical question from a book about generics, but the question does not make sense to me. Here it is.

Create two classes with the same functionality. Use generics for the first class and pass the second class to object types. Create a for loop that uses a class and an object-based class to determine what works best.

I'm not sure what this means by going over to types of objects. Here is my code so far

   //Generic
    class Person<T> {

        T var1;

        public Person(T yer) {
            var1 = yer;
        }

        public T Value { get { return var1; } }
    }

    //Normal class
    class Human {

        int var1;

        public Human(int yer) {
            var1 = yer;
        }

        public int Value { get { return var1; } }
    }

My main looping program

for (int i = 0; i < 1000000; i++) {
                Person<int> me = new Person<int>(1);
                int hey = me.Value;
            }

            for (int i = 0; i < 1000000; i++) {
                Human per = new Human(1);
                object her = (object)per.Value;
            }

I don’t know if I will do it right. Help me please: -)

+1
source share
8 answers

I think this question asks you to create a collection class and insert instances of your class into it.

,

:

List<Human> myList = new List<Human>();
Human h = new Human();
myList.Add(h);

:

ArrayList myObjectList = new ArrayList();
Human h = new Human();
myObjectList.Add((object)h));

, .

+6

, , .

Generic

List<Person> pList = new List<Person>();
for(int i = 0; i<1000; ++i)
    pList.Add(new Person(30));

StopWatch sw = new StopWatch();
sw.start();
int sum = 0;
foreach(Person p in pList)
    sum += p.Value;
sw.Stop();

ArrayList hList = new ArrayList;
for(int i = 0; i<1000; ++i)
    hList.Add(new Human(30));

StopWatch sw = new StopWatch();
sw.start();
int sum = 0;
foreach(Object h in hList)
    sum += ((Human)h).Value;
sw.Stop();
+1

, , , Person < > . , , Var1 . , var1, int:

class Human 
{        
     object var1;
     public Human(int yer) 
     {            
          var1 = (object) yer;        
     }
     public int Value 
     { 
         get { return (int) var1;     }
     }    
}

, var1 - , int, , , Human. , , .

+1

, , for. Int - ,

object her = (object)per.Value;

int her = per.Value;

, , . Timer, , , , .

0

, , unboxing .

int = 123; o = () i;//

o i:

o = 123; i = (int) o;//unboxing

, .

0

, . Object, . : Equals, GetHashCode, GetType ToString. , Equals , .

0

, , , (DateTime.Now.Ticks), .

endian-, , .

, .

.

0

, , DateTime.Now.Ticks, :

        DateTime t = DateTime.Now;
        for (int i = 0; i < 1000000; i++)
        {
            Person<int> me = new Person<int>(1);
            int hey = me.Value;
        }
        long a = DateTime.Now.Ticks - t.Ticks;
        TimeSpan A = new TimeSpan(a);


        for (int i = 0; i < 1000000; i++)
        {
            Human per = new Human(1);
            object her = (object)per.Value;
        }

        long b = DateTime.Now.Ticks - t.Ticks;
        TimeSpan B = new TimeSpan(b);

        Console.WriteLine(A.ToString());
        Console.WriteLine(B.ToString());
        Console.ReadLine();

There is a big difference in performance, since generics are about twice as fast.

0
source

All Articles