Solved

C#: Generics

Posted on 2014-03-21
5
226 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 62

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now