Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Alternative to Activator.CreateInstance

Rate me:
Please Sign up or sign in to vote.
2.88/5 (3 votes)
31 Jan 2012CPOL 8.7K   3
And why don't you just do this?public static T Create() where T : class, new(){ return new T();}

And why don't you just do this?


C#
public static T Create<T>() where T : class, new()
{
    return new T();
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect CGI
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
BillWoodruff3-Feb-15 23:14
professionalBillWoodruff3-Feb-15 23:14 
QuestionMy vote of 5 Pin
Volynsky Alex12-Jan-15 1:48
professionalVolynsky Alex12-Jan-15 1:48 
GeneralReason for my vote of 1 Have you even taken the time to read... Pin
Dean Oliver31-Jan-12 18:20
Dean Oliver31-Jan-12 18:20 
Reason for my vote of 1
Have you even taken the time to read this tip and trick. The whole point of this tip is to instantiate an object dynamically at runtime when you don't know the type. Here you are passing in a type at compile-time that you will know. This a very poor alternative no thought and research was done.

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.