CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2011
    Location
    .Net 4.0
    Posts
    39

    [RESOLVED] What exactly are the <> doing?

    DataTable<Customer> Customers=db.GetTable<Customer>();

    I understand the simple DataTable myTable = new DataTable;

    Please describe what adding <Customer> is called, how the system handles it and what it is doing. Also, where else might this be used?

    Thanks.

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: What exactly are the <> doing?

    This is an example of C# generics. A good introduction is here: http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx

    Long story short, the <> allow you to pass a data type as a parameter. So for example a generic list (List<>) can declare like:

    Code:
    List<int> myList = new List<int>();
    This is a list of items of type int. If we wanted to create a list of doubles, we could instead do:

    Code:
    List<double> myList = new List<double>();
    This is useful because it allows a programmer to define a single, data-type-agnostic method (or class) for all data types.

    In your example, it means that a DataTable is being declared that holds records of type Customer. The same generic class (DataTable) can also be used to hold other types of records (e.g. int, double). This prevents the need to write multiple objects to hold different types of records (e.g. it avoids CustomerDataTable, IntDataTable, DoubleDataTable, etc needing to be separately declared).

    Note also that you can have multiple parameters. For example Dictionary<T,U> is a generic hashtable that takes two parameters, T and U that specify the input type and the output type. For example:

    Code:
    Dictionary<string, int> map = new Dictionary<string, int>();
    map.Add("some string", 5);
    map.Add("another string", 12);
    
    int val = map["some string"]; //val will be assigned 5
    Here we have a hash table that accepts a string as an input parameter and maps this to a int.

    Hopefully that makes some sense. Generics are very powerful. I recommend you read the above link to fully understand them!
    Last edited by BioPhysEngr; December 23rd, 2011 at 01:45 AM. Reason: more detail
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Dec 2011
    Location
    .Net 4.0
    Posts
    39

    Re: What exactly are the <> doing?

    Thanks, Bio. It sort-of makes sense. Your explanation is better for a newbie tan the long article at the link.

    But what I don't understand is, Why add the <int> to List in the first place? Why not just List by itself? What happens if you do not use the generic?

    What would be 1 or 2 simple, easy to understand samples to demonstrate this?

    Thanks.

  4. #4
    Join Date
    Dec 2011
    Location
    .Net 4.0
    Posts
    39

    Re: [RESOLVED] What exactly are the <> doing?

    I did some additional reading. Thanks for giving me the keyword "Generics." I pretty much get it, now.

  5. #5
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: [RESOLVED] What exactly are the <> doing?

    Because the List needs to know what it is listing. Let's say I tell you "grab a pen and paper and make a list"... the first thing you'd ask is "a list of what?"

    < > define what type of data the list will be containing. In order to be able to add, search and return a record, it has to know what it is searching for. The List, once defined, is not generic anymore, it is a list of integers, or doubles, or whatever, and cannot be treated otherwise.

  6. #6
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: [RESOLVED] What exactly are the <> doing?

    The old way of implementing a "generic" (i.e. accepting any object; not using generics) list would have been to create a list of objects (from which every class derives). To get back the Object-Of-Interest, you'd have to cast when reading items out:

    Code:
    ArrayList myList = new ArrayList();
    myList.Add("hello");
    
    string item = (string)myList[0];
    The casting operation is expensive and the list isn't type safe. Generics solve both problems (no need to cast and allows for type safety).
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured