Solved

C#: Generics

Posted on 2014-03-21
5
228 Views
Last Modified: 2014-03-21
I am a beginner in C#, and just started learning about Generics. I was doing some code review and ran across this:
List<string> myList = new List<string>();

Open in new window

Just by looking at it, how can I determine if it's a generic list or a regular list?

Also, the book says a Generic type's naming convention says the type should start with a T. What if the code is not following normal naming conventions. Is there anything that tells you this is a generic type -- without writing any code to check if it is or isn't.
0
Comment
Question by:pzozulka
5 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 250 total points
ID: 39946417
When you see List<someType> it's a generic type. A regular List doesn't actually exist in the .NET framework. A non-generic list in .NET is an ArrayList.

The naming convention is for when you're creating the generic type, not using it. If you were creating a type to store key/value pairs you might code something like this:

public class KeyValuePair<TKey, TValue>
{
    public TKey Key { get; set; }

    public TValue StoredValue { get; set; }
}

Open in new window


Then when someone went to use your class they would use it as:

KeyValuePair<int, string> = new KeyValuePair<int, string>();

Open in new window


Now the key always has to be an int and the value has to be a string.
0
 
LVL 8

Author Comment

by:pzozulka
ID: 39946434
A regular List doesn't actually exist in the .NET framework.
Are you saying I can't do:
List myL = new List();

Open in new window

0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 39946471
This, List<string> myList = new List<string>();, defines a strongly typed collections of System.String. The data type inside < and > will determine the type of the collection. A Generic collection will have a place holder for the type such as this, List<T> myList = new List<T>();, Here is a generic function :
static void Swap<T>(ref T lhs, ref T rhs)
{
    T temp;
    temp = lhs;
    lhs = rhs;
    rhs = temp;
}

Open in new window


To use the above generic function in your code you can do this:
int a = 1;
int b = 2;

Swap<int>(ref a, ref b);
System.Console.WriteLine(a + " " + b);

Open in new window

Note that when Swap is called it tells the Generic function what the type of T is. This makes the function re-useable by any type and therefore the code needs only to be written only once.

In this example you could also call Swap like so:
Swap(ref a, ref b);

Open in new window

Because the compiler will infer the data type. By the way T can be anything you want it to be, it is just a place holder for the type.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39946480
By the way T can be anything you want it to be...
I would add, though, that there are ways to restrict the type(s) to fitting a certain criteria. Generic type constraints can be used to restrict the types that can be used with your custom generic classes.

**I mention generic type constraints only to make you aware of them. Focus on the basics first. Once you grasp those, then move on to constraints.
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39946505
Are you saying I can't do:
List myL = new List();

Have you tried it? What happened when you did? If you're wondering what's going to happen if you try something, just try it. You'll learn more than having someone spoon-feed you the answer. Besides, the compile is a much better compiler than asking people on a forum to compile your code.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question