| Author: Vishnu K J 20 Aug 2008 | Member Level: Gold | Rating: Points: 6 |
A class is simply a representation of a type of object; think of it as a blueprint that describes the object. Just as a single blueprint can be used to build multiple buildings, a class can be used to create multiple copies of an object.
An instance of a class is called an object.
Generics simply means something that is not tied to any sort of brand name. For example, if we purchase some generic dish soap, soap that has no brand name on it, we know that we are buying dish soap and expect it to help us clean our dishes, but we really don't know what exact brand (if any) will be inside the bottle itself. We can treat it as dish soap even though we don't really have any idea of its exact contents.
if we want to handle our Types in a generic manner, we always need to cast them to a System.Object, and we lose any benefit of a rich-typed development experience. For example, I'm sure most of us are familiar with the System.Collection.ArrayList class in Framework v1 and v1.1. If you have used it at all, you will notice a few things about it:
1. It requires that you store everything in it as an object
public virtual int Add(object value); public virtual object this[int index] {get; set;}
2. You need to cast up in order to get your object back to its actual Type 3. Performance is really lacking, especially when iterating with foreach()
4. It performs no type safety for you (no exceptions are thrown even if you add objects of different types to a single list)
System.Collections.ArrayList list = new System.Collections.ArrayList();
list.Add("a string"); list.Add(45); //no exception thrown list.Add(new System.Collections.ArrayList()); //no exception
foreach(string s in list) { //exception will be thrown! System.Console.WriteLine(s); }
|
| Author: D.Jeya kumar(JK) 20 Aug 2008 | Member Level: Diamond | Rating: Points: 5 |
Hi,
Class Defination:
A class is Blue print(representation) from which objects are created.
http://msdn.microsoft.com/en-us/library/ee5edha0(VS.80).aspx
http://java.sun.com/docs/books/tutorial/java/concepts/index.html
Object Defination: An object is an instance of the class.
http://msdn.microsoft.com/en-us/library/ee5edha0(VS.80).aspx
Generics
Generics defination is given in the below link
http://msdn.microsoft.com/en-us/library/ms172193.aspx
regards JK
http://www.ondotnet.com/pub/a/dotnet/2004/05/17/liberty.html?page=2
|
| Author: kumar 20 Aug 2008 | Member Level: Bronze | Rating: Points: 6 |
class refers logical representation of data. objects refers the instance(copy) of class.
Generics mainly use to providing data types at runtime.generic programming that allow some code to effectively circumvent the static typing requirements. Generic programming is commonly used to implement containers such as lists and hash tables and functions such as a particular sorting algorithm for objects specified in terms more general than a concrete type.
Example ---------- C# (and .NET in general) allows a wide range of generic type constraints using the where keyword including restricting generic types to be value types, to be classes, to have constructors, and to inherit from interfaces.[3] Below is an example with an interface constraint:
class Sample { public static void Main() { int[] list = { 0, 1, 2, 3 }; MakeAtLeast<int>(list, 2); // change array to { 2, 2, 2, 3 } } public static void MakeAtLeast<T>(IList<T> list, T lowest) where T : IComparable<T> { for (int i = 0; i < list.Count; i++) if (list[i].CompareTo(lowest) < 0) list[i] = lowest; } }
Since all arrays implement the IList<T> interface (commonly associated with list classes), the MakeAtLeast() method allows operation on arrays, with elements of type T. The method's type constraint indicates that the method is applicable to any type T that implements the generic IComparable<T> interface. This ensures a compile time error if the method is called with a list of another type. The interface provides the generic method CompareTo(T).
The above method could also be written without generic types, simply using the non-generic IList type. However, this would make the method less type safe, as it would allow the method to be applied to a list of incomparable items, resulting in a run time error. The method would need to access the list items as objects instead, and would require casting to compare two elements. (For value types like types such as int this requires a boxing conversion, although this can be worked around using the Comparer<T> class, as is done in the standard collection classes.)
related sites http://www.15seconds.com/issue/031024.htm http://msdn.microsoft.com/en-us/library/aa479859.aspx http://msdn.microsoft.com/en-us/library/512aeb7t.aspx
|
| Author: Sridhar R 20 Aug 2008 | Member Level: Diamond | Rating: Points: 5 |
Generics.. The main purpose of generics is to make the code in software component more reusable.Generics permit classes, structs, interfaces, delegates, and methods to be parameterized by the types of data they store and manipulate.
For More Details.. http://msdn.microsoft.com/en-us/library/512aeb7t.aspx http://msdn.microsoft.com/en-us/library/512aeb7t(VS.80).aspx http://www.codeproject.com/KB/cs/gencsharp.aspx http://www.c-sharpcorner.com/UploadFile/sdhar8po/GenericsInCSharp11152005055344AM/GenericsInCSharp.aspx
|
| Author: Bindu Bujji 28 Sep 2008 | Member Level: Gold | Rating: Points: 5 |
Hi, Refer this site. You will understand very easily. Infact just now i learned what a Generic. Very good info with good examples.
http://en.csharp-online.net/CSharp_Generics_Recipes http://en.csharp-online.net/CSharp_Generics_Recipes%E2%80%94Deciding_When_and_Where_to_Use_Generics_Problem http://en.csharp-online.net/CSharp_Generics_Recipes%E2%80%94Understanding_Generic_Types
This definitely will helps Have a Good one Bindu
|