开发者

value of generic class?

开发者 https://www.devze.com 2023-01-23 04:49 出处:网络
I couldn\'t find the need of a generic class other than situations like collection ofclasses. Items like stack and linked list.

I couldn't find the need of a generic class other than situations like collection of classes. Items like stack and linked list.

I can see people use it in other places开发者_开发问答 also.

What do we gain using a generic class?


Well let's say you have a class that repersents a database table, you would need methods like Save(), Delete() - which work on any database table entity, and regardless of the type, they all operate the same way (a generic way).

So to solve that you could use a generic class like so:

public class BaseEntityModel<T> where T : IEntity2 // We use llblgen here
{
     // ... code

     // T is a generic type parameter which has to implement IEntity2.
     // But we don't care what type it is.
     public T Entity { get; set; }

     public void Save()
     {
           Entity.Save(); // Save is from IEntity2
     }

     // ... code
}

Then for a Customer class we could go like this

public class Customer : BaseEntityModel<CustomerEntity>
{
    public void Method()
    {
        // Entity is of type Customer entity
        Entity.CustomerName = "Bob";
        base.Save();
    }

}

and then one for Company

public class Company : BaseEntityModel<CompanyEntity>
{
     public void Method()
      {
        // Entity is of type Company entity
        Entity.CompanyName = "My Company";
        base.Save(); // Save is declared in the base class for a generic type (in this case company)
     }

}

So is this code example it allows the same generic code to be used on different types of objects, and can be constrained to certain types of objects (using where T : IMyInterface)

There are more ways to constrain the type of object that can me used on. This information is on MSDN


MSDN has a pretty comprehensive article on the Advantages and Limitations of Generics. Basically, when you use generics you decrease the headache of type-checking to ensure type safety, type compatibility, etc. It's all handled for you at compile time.

They can help you to avoid code duplication -- for example, rather than writing FooMethod(int arg), FooMethod(string arg) etc., you can just write FooMethod<T>(T arg), which is cleaner and much more maintainable.

Also note that you can add constraints on generic type parameters using the where keyword. This is useful in making sure that a generic type parameter implements a certain interface, or has a default parameterless constructor, etc.


You gain code reuse - the same source code and logic, instantiated using and operating on different concrete types or combination of types. Leverage.


Generics allow you to implement type-safe routines that will work on any type that supports the operations performed in the routine. This way you don't have to define the same class or function multiple times to handle different types even though all the types would be handled exactly the same way in the source.


You are more-or-less right: apart from very weird places, you mainly use them in collections. But you use collections everywhere, so generics are a godsend.

0

精彩评论

暂无评论...
验证码 换一张
取 消