Kerala Microsoft Users Group

Windows 7 Deployability

Static generic class

Latest post 06-30-2009 4:19 AM by Anuraj. 3 replies.
  • 06-29-2009 7:50 AM

    Static generic class

    Hi All

    I am planning to write a Static Generic class, for Serialization. Is there any problem if I create a Static Generic class? Because some one in my team told that its not a good practise to make static classes Generic. Its utility class and chances of using this class as base class is less.

     

    Thanks

    Anuraj P
    http://www.dotnetthoughts.net

    THIS POSTING IS PROVIDED "AS IS" WITH NO WARRANTIES, AND CONFERS NO RIGHTS.
    BEWARE OF BUGS IN THE ABOVE CODE; I HAVE ONLY PROVED IT CORRECT, NOT TRIED IT.

    Filed under: , , ,
    • Post Points: 45
  • 06-30-2009 1:37 AM In reply to

    Re: Static generic class

    Reply |Contact |Answer

    I cant forsee any problem using static class as generics. One thing you need to keep track is, for different parameter you passing to the generic class, a different type will be created.

    Also take care of thread safety.

    • Post Points: 5
  • 06-30-2009 3:22 AM In reply to

    Re: Static generic class

    Reply |Contact |Answer

    I too do not see any issues in using static generic classes.

    >> The most common use for generic classes is with 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 much the same way regardless of the type of data being stored.

    http://msdn.microsoft.com/en-us/library/sz6zd40f(VS.80).aspx

    When creating your own generic classes, important considerations include:

    • Which types to generalize into type parameters.

      As a general rule, the more types you are able to parameterize, the more flexible and reusable your code becomes. However, too much generalization can result in code that is difficult for other developers to read or understand.

    • What constraints, if any, to apply to the type parameters (See Constraints on Type Parameters (C# Programming Guide)).

      A good rule is to apply the maximum constraints possible that will still let you handle the types you need to handle. For example, if you know that your generic class is intended for use only with reference types, then apply the class constraint. That will prevent unintended use of your class with value types, and will enable you to use the as operator on T, and check for null values.

    • Whether to factor generic behavior into base classes and subclasses.

      Since generic classes can serve as base classes, the same design considerations apply here as with non-generic classes. See below for rules on inheriting from generic base classes.

    • Whether to implement one or more generic interfaces.

      For example, if you are designing a class that will be used to create items in a generics-based collection, you may need to implement an interface such as IComparable<T> where T is the type of your class.

    PraVeeN
    Microsoft MVP
    blog.ninethsense.com | kidoos.net

    • Post Points: 25
  • 06-30-2009 4:19 AM In reply to

    Re: Static generic class

    Thanks Amal, Praveen for the response.

    Instead of creating Static Generic class, I created some extention methods. Smile

    Thanks

    Anuraj P
    http://www.dotnetthoughts.net

    THIS POSTING IS PROVIDED "AS IS" WITH NO WARRANTIES, AND CONFERS NO RIGHTS.
    BEWARE OF BUGS IN THE ABOVE CODE; I HAVE ONLY PROVED IT CORRECT, NOT TRIED IT.

    • Post Points: 5
Page 1 of 1 (4 items) | RSS