Click here to Skip to main content
16,007,472 members
Home / Discussions / C#
   

C#

 
Questionautomation Pin
s_khan_87627-May-07 20:14
s_khan_87627-May-07 20:14 
AnswerRe: automation Pin
blackjack215027-May-07 21:11
blackjack215027-May-07 21:11 
GeneralRe: automation Pin
s_khan_87627-May-07 21:26
s_khan_87627-May-07 21:26 
GeneralRe: automation Pin
blackjack215027-May-07 22:26
blackjack215027-May-07 22:26 
Questiongenerics in c# Pin
Ashwani_kumar27-May-07 19:47
Ashwani_kumar27-May-07 19:47 
AnswerRe: generics in c# Pin
Ashwani_kumar27-May-07 19:48
Ashwani_kumar27-May-07 19:48 
GeneralRe: generics in c# Pin
Ashwani_kumar27-May-07 19:48
Ashwani_kumar27-May-07 19:48 
GeneralRe: generics in c# Pin
Sandeep Akhare27-May-07 20:11
Sandeep Akhare27-May-07 20:11 
Confused | :confused: Confused | :confused: D'Oh! | :doh: D'Oh! | :doh:
Whats this ? question is yours and replies are also from your side

Take a look
Generics are classes, structures, interfaces, and methods that have placeholders (type parameters) for one or more of the types they store or use. A generic collection class might use a type parameter as a placeholder for the type of objects it stores; the type parameters appear as the types of its fields, and the parameter types of its methods. A generic method might use its type parameter as the type of its return value, or as the type of one of its formal parameters. The following code illustrates a simple generic class definition.

Visual Basic Copy Code
Public Class Generic(Of T)
Public Field As T
End Class

C# Copy Code
public class Generic<t>
{
public T Field;
}

C++ Copy Code
generic<typename t=""> public ref class Generic
{
public:
T Field;
};


When you create an instance of a generic class, you specify the actual types to substitute for the type parameters. This establishes a new generic class, referred to as a constructed generic class, with your chosen types substituted everywhere that the type parameters appear. The result is a type-safe class tailored to your choice of types, as the following code illustrates.

Visual Basic Copy Code
Dim g As New Generic(Of String)
g.Field = "A string"

C# Copy Code
Generic<string> g = new Generic<string>();
g.Field = "A string";

C++ Copy Code
Generic<string^>^ g = gcnew Generic<string^>();
g->Field = "A string";


The following terms are used to talk about generics in the .NET Framework:

A generic type definition is a class, structure, or interface declaration that functions as a template, with placeholders for the types it can contain or use. For example, the Dictionary class can contain two types: keys and values. Because it is only a template, you cannot create instances of a class, structure, or interface that is a generic type definition.

Generic type parameters, or type parameters, are the placeholders in a generic type or method definition. The Dictionary generic type has two type parameters, TKey and TValue, representing the types of its keys and values.

A constructed generic type, or constructed type, is the result of specifying types for the generic type parameters of a generic type definition.

A generic type argument is any type that is substituted for a generic type parameter.

The general term "generic type" includes both constructed types and generic type definitions.

Constraints are limits placed on generic type parameters. For example, you might limit a type parameter to types that implement the IComparer generic interface, to ensure that instances of the type can be ordered. You can also constrain type parameters to types that have a particular base class, that have a default constructor, or that are reference types or value types. Users of the generic type cannot substitute type arguments that do not satisfy the constraints.

A generic method definition is a method with two parameter lists: a list of generic type parameters, and a list of formal parameters. Type parameters can appear as the return type or as the types of the formal parameters, as in the following code.

Visual Basic Copy Code
Function Generic(Of T)(ByVal arg As T) As T
Dim temp As T = arg
...
End Function



C# Copy Code
T Generic<t>(T arg) { T temp = arg; ...}



C++ Copy Code
generic<typename t=""> T Generic(T arg) { T temp = arg; ...};



Generic methods can appear on generic or nongeneric types. It is important to note that a method is not generic just because it belongs to a generic type, or even because it has formal parameters whose types are the generic parameters of the enclosing type. A method is generic only if it has its own list of type parameters. In the following code, only method G is generic.

Visual Basic Copy Code
Class A
Function G(Of T)(ByVal arg As T) As T
...
End Function
End Class
Class Generic(Of T)
Function M(ByVal arg As T) As T
...
End Function
End Class



C# Copy Code
class A
{
T G<t>(T arg) {...}
}
class Generic<t>
{
T M(T arg) {...}
}



Thanks and Regards
Sandeep

If If you look at what you do not have in life, you don't have anything,
If you look at what you have in life, you have everything... "




AnswerRe: generics in c# Pin
Judah Gabriel Himango27-May-07 19:58
sponsorJudah Gabriel Himango27-May-07 19:58 
AnswerRe: generics in c# Pin
Sathesh Sakthivel27-May-07 20:10
Sathesh Sakthivel27-May-07 20:10 
Questioncalling the c# class in VB6.0. (Urgent)????? Pin
Ron.S27-May-07 19:31
Ron.S27-May-07 19:31 
AnswerRe: calling the c# class in VB6.0. (Urgent)????? Pin
Rahul.RK27-May-07 19:35
Rahul.RK27-May-07 19:35 
AnswerRe: calling the c# class in VB6.0. (Urgent)????? Pin
Christian Graus27-May-07 21:23
protectorChristian Graus27-May-07 21:23 
GeneralRe: calling the c# class in VB6.0. (Urgent)????? Pin
Ron.S27-May-07 22:57
Ron.S27-May-07 22:57 
QuestionGetting and Setting location of Dockable window Pin
kumar.bs27-May-07 18:23
kumar.bs27-May-07 18:23 
QuestionWriting in an embedded XML file Pin
xbiplav27-May-07 17:46
xbiplav27-May-07 17:46 
AnswerRe: Writing in an embedded XML file Pin
Christian Graus27-May-07 18:11
protectorChristian Graus27-May-07 18:11 
QuestionLock a textbox and richtextbox together Pin
simplicitylabs27-May-07 11:04
simplicitylabs27-May-07 11:04 
AnswerRe: Lock a textbox and richtextbox together Pin
Christian Graus27-May-07 11:57
protectorChristian Graus27-May-07 11:57 
GeneralRe: Lock a textbox and richtextbox together Pin
simplicitylabs27-May-07 12:19
simplicitylabs27-May-07 12:19 
GeneralRe: Lock a textbox and richtextbox together Pin
Christian Graus27-May-07 12:36
protectorChristian Graus27-May-07 12:36 
GeneralRe: Lock a textbox and richtextbox together Pin
simplicitylabs27-May-07 12:53
simplicitylabs27-May-07 12:53 
GeneralRe: Lock a textbox and richtextbox together Pin
Christian Graus27-May-07 13:08
protectorChristian Graus27-May-07 13:08 
AnswerRe: Lock a textbox and richtextbox together Pin
Arun.Immanuel27-May-07 18:56
Arun.Immanuel27-May-07 18:56 
AnswerRe: Lock a textbox and richtextbox together Pin
mav.northwind27-May-07 23:08
mav.northwind27-May-07 23:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.