Solved

C#: Generics

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

705 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