Show

FAQs of level

Generics are classes, structures, interfaces, and methods that have placeholders (type parameters) for one or more of the types that they store or use. A generic collection class might use a type parameter as a placeholder for the type of objects that 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

public class Generic

 {   public T Field;}


public static void Main()

{

    Generic g = new Generic();

    g.Field = "A string";

    Console.WriteLine("Generic.Field           = "{0}"", g.Field);

    Console.WriteLine("Generic.Field.GetType() = {0}", g.Field.GetType().FullName);

}

Generic classes encapsulate operations that are not specific to any particular data type. The most common use for generic classes is with the collections like linked lists, hash tables, stacks, queues, trees and so on where operations such as adding and removing items from the collection are performed in more or less the same way regardless of the type of the data being stored.

The advantage of generics is better type safety, code re-usability and better performance. Generics are most commonly used to create a collection

Dispose: This method uses interface – “IDisposable” interface and it will free up both managed and unmanaged codes like – database connection, files etc.
Finalize: This method is called internally unlike Dispose method which is called explicitly. It is called by garbage collector and can’t be called from the code.


Advantages of generics are:

  • Reusability 
  • Improves Performance

You put an @ sign in front of the double-quoted string.

String ex = @"This has a carriage return "

The sealed keyword prohibits a class from being inherited.

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*