?
Solved

C#: Generics

Posted on 2014-03-21
5
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 1000 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 1000 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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

752 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