How to I Start with C# Generics?

A

Anonieko Ramos

The logical thing to do is to try it yourself.

See this example:
-----------------------------------------------------------

using System;
using System.Collections.Generic;

namespace Tests
{

public class Employee
{
int id;
string name;

public Employee(int anId, string aName)
{
this.id = anId;
this.name = aName;
}

public int Id
{
get { return this.id; }
}

public string Name
{
get { return this.name; }

set { this.name = value; }
}
}

}



namespace Tests
{

public class GenericReferenceArrayListTest {

static void Main()
{
DateTime start = DateTime.Now;

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

Employee emp = new Employee(101, "Smith, Joan");


for (int i = 0; i < 1000000; i++)
{
employees.Add(emp);
}

foreach (Employee e in employees)
{
Employee currentEmployee = e;
}

DateTime finish = DateTime.Now;

System.Console.WriteLine(String.Format("Elapsed time: {0}",
finish.Subtract(start)));
}

}

}
 
L

Lingeshwaran Palaniappan

Hi,
as suggested the logical thing to do would be to try yourself.
You might find this link useful:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html
/csharp_generics.asp
--------------------
From: (e-mail address removed) (Anonieko Ramos)
Newsgroups: microsoft.public.dotnet.languages.csharp
Subject: How to I Start with C# Generics?
Date: 24 Jul 2004 13:23:28 -0700
Organization: http://groups.google.com
Lines: 77
Message-ID: <[email protected]>
NNTP-Posting-Host: 68.161.38.231
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1090700609 1116 127.0.0.1 (24 Jul 2004 20:23:29 GMT)
X-Complaints-To: (e-mail address removed)
NNTP-Posting-Date: Sat, 24 Jul 2004 20:23:29 +0000 (UTC)
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!tiscali!newsfeed1.ip.tiscali.net!proxad.net!postnews2.google.com!not-fo
r-mail
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:261009
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

The logical thing to do is to try it yourself.


See this example:
-----------------------------------------------------------

using System;
using System.Collections.Generic;

namespace Tests
{

public class Employee
{
int id;
string name;

public Employee(int anId, string aName)
{
this.id = anId;
this.name = aName;
}

public int Id
{
get { return this.id; }
}

public string Name
{
get { return this.name; }

set { this.name = value; }
}
}

}



namespace Tests
{

public class GenericReferenceArrayListTest {

static void Main()
{
DateTime start = DateTime.Now;

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

Employee emp = new Employee(101, "Smith, Joan");


for (int i = 0; i < 1000000; i++)
{
employees.Add(emp);
}

foreach (Employee e in employees)
{
Employee currentEmployee = e;
}

DateTime finish = DateTime.Now;

System.Console.WriteLine(String.Format("Elapsed time: {0}",
finish.Subtract(start)));
}

}

}


--
Lingeshwaran Palaniappan, Developer Division

This posting is provided "AS IS" with no warranties, and confers no rights.

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top