[go: up one dir, main page]

0% found this document useful (0 votes)
29 views16 pages

Generics in C#

Uploaded by

rija.rajagiri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views16 pages

Generics in C#

Uploaded by

rija.rajagiri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

GENERICS IN C#

contents
• Introduction to generics
• Benefits of generics
• Generic Class
• Generic Interface
• Generic Method
• Generic Delegate
Introduction to Generics

• Generics introduced in C# 2.0. Generics allow you to define a


class with placeholders for the type of its fields, methods,
parameters, etc. Generics replace these placeholders with
some specific type at compile time.

• A generic class can be defined using angular brackets <>. For


example, the following is a simple generic class with a generic
member variable, generic method and property.
Benefits
• Increases the reusability of the code.

• Generic are type safe. You get compile time errors if you try to use a
different type of data than the one specified in the definition.

• Generic has a performance advantage because it removes the possibilities


of boxing and unboxing.

LinkedList<string> llist = new LinkedList<string>();


Generic Class

class MyGenericClass<T>
{
private T genericMemberVariable;
public MyGenericClass(T value)
{
genericMemberVariable = value;
}
public T genericMethod(T genericParameter)
{
Console.WriteLine("Parameter type: {0}, value: {1}", typeof(T).ToString(),genericParameter);
Console.WriteLine("Return type: {0}, value: {1}", typeof(T).ToString(), genericMemberVariable);
Return genericMemberVariable;
}
public T genericProperty
{
get; set;
}
}
Generic Delegates
• The delegate defines the signature of the method which it
can invoke. A generic delegate can be defined the same way
as delegate but with generic type.

• A generic delegate can point to methods with different


parameter types. However, the number of parameters should
be the same.
Points to Remember

1.Generics denotes with angular bracket <>.

2.Compiler applies specified type for generics at compile time.

3.Generics can be applied to interface, abstract class, method, static


method, property, event, delegate and operator.

4.Generics performs faster by not doing boxing & unboxing.


Anonymous methods
In C#
• An anonymous method is a method without a name.

• Anonymous methods in C# can be defined using the delegate keyword and can be assigned to a variable of delegate type.

• Example:
• Anonymous methods can access variables defined in an outer
function.
• Anonymous methods can also be passed to a method that accepts
the delegate as a parameter.
• Anonymous methods can be used as event handlers:
saveButton.Click += delegate(Object o, EventArgs e)
{
System.Windows.Forms.MessageBox.Show("Save
Successfully!");
};
Typical usage of an anonymous method is to assign it to an event.
Limitations
• It cannot contain jump statement like goto, break or continue.
• It cannot access ref or out parameter of an outer method.
• It cannot have or access unsafe code.
• It cannot be used on the left side of the is operator.
Points to Remember
• Anonymous method can be defined using the delegate keyword
• Anonymous method must be assigned to a delegate.
• Anonymous method can access outer variables or functions.
• Anonymous method can be passed as a parameter.
• Anonymous method can be used as event handlers.

You might also like