Solved

C#: Generics

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

17 Experts available now in Live!

Get 1:1 Help Now