Click here to Skip to main content
15,124,353 members
Articles / Programming Languages / C#
Alternative
Tip/Trick
Posted 31 Jan 2012

Tagged as

Stats

7.8K views

Alternative to Activator.CreateInstance

Rate me:
Please Sign up or sign in to vote.
2.88/5 (3 votes)
31 Jan 2012CPOL
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)

Share

About the Author

Yves Vaillancourt
Architect CGI
Canada Canada
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pin
BillWoodruff4-Feb-15 0:14
mveBillWoodruff4-Feb-15 0:14 
QuestionMy vote of 5 Pin
Volynsky Alex12-Jan-15 2:48
professionalVolynsky Alex12-Jan-15 2:48 
GeneralReason for my vote of 1 Have you even taken the time to read... Pin
Dean Oliver31-Jan-12 19:20
MemberDean Oliver31-Jan-12 19: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.