473,321 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

What is meant by Generics?

What is ment by Generics? How we can define as Generics in Programaticall way?
Jun 5 '08 #1
5 1348
r035198x
13,262 8TB
Some reading is required here.

Generally they help in type checking. i.e Using generics allows the compiler to help you check the correctness of your code by inferring information from the specified types. They also make your programming easier by not having to put a lot of casts. e.g
if you do
Expand|Select|Wrap|Line Numbers
  1. ArrayList a = new ArrayList();
  2. a.Add("test");
You cannot do
Expand|Select|Wrap|Line Numbers
  1. string s = a[0]; 
even if you know that the object there is a string.
You have to use
Expand|Select|Wrap|Line Numbers
  1. string s = (string)a[0];
With generics that is not required, you simply do
Expand|Select|Wrap|Line Numbers
  1. List<string> a = new List();
  2. a.Add("test");
  3. string s = a[0];
From now on the compiler will be helping you by inserting the casts where required and also stopping you from putting non-strings in that List.
Jun 5 '08 #2
Good example. Generics also promote flexibility and performance. Remember when you cast (string) or (int), that is a computationally expensive process, something like 4 times slower than just assigning a value.
Jun 5 '08 #3
r035198x
13,262 8TB
Good example. Generics also promote flexibility and performance. Remember when you cast (string) or (int), that is a computationally expensive process, something like 4 times slower than just assigning a value.
I missed the performance part because I had my Java cap when I typed that reply. (In Java erasure replaces all those generic types with raw types for interoperability with legacy code)
Jun 5 '08 #4
cloud255
427 Expert 256MB
Correct me if i am wrong, but as far as i can tell C# generics are exactly the same thing as C++ Templates?
Jun 23 '08 #5
r035198x
13,262 8TB
Correct me if i am wrong, but as far as i can tell C# generics are exactly the same thing as C++ Templates?
"C# generics vs C++ templates" gives some interesting pages on Google.
Jun 23 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
20
by: yaron | last post by:
Hi, 1. I am familiar with .NET 1.1 and not with .NET 2.0 . if i should start a new networking project should i write it with .net 2.0 beta or write it with .net 1.1 ? 2. What are the...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
19
by: Fernando Cacciola | last post by:
I'm puzzled, Why and how _exactly_ is this: void Foo<T>(T v ) where T : Interface/Value/Class/class any better than this void Foo( Interface/Value/Class/object v )
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
6
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.