Is it possible to instantiate an object of generic type in Java or C#?

This is possible in C# if you are setting constraint on T type to have parameterless constructor public class MyClass where T:new(){ public void aMethod { .. T object = new T(); .. } .. } See here.

This is possible in C# if you are setting constraint on T type to have parameterless constructor. Public class MyClass where T:new(){ public void aMethod { .. T object = new T(); .. } .. } See here.

Putting restrictions on type is not a generic solution. – Developer Art Oct 16 '09 at 14:22 2 yes, it is not. But it provides compile-time check which is impossible when using Activator.

And it fits requirements of the original poster ;) – elder_george Oct 16 '09 at 15:00 3 @Developer Art: “generic” has a fixed meaning in the context of the question in both C# and Java. Don’t redefine it arbitrarily just for the sake of controversy. This solution is generic, period.

– Konrad Rudolph Oct 16 '09 at 15:22.

Yes, it's possible. You can use Activator.CreateInstance(); From MSDN: Activator. CreateInstance(T) Method Creates an instance of the type designated by the specified generic type parameter, using the parameterless constructor.

Like Donut said, C# allows you to instantiate arbitrary types at runtime using Activator. CreateInstance which works a bit like Class. NewInstance in Java.

Since C#’s Generics are preserved after compilation, C#, unlike Java, allows you to get hold of a Type instance for a given type parameter and thus call CreateInstance or any other constructor. For the parameterless constructor, this is considerably easier, using a generic constraint: void T createT() where T : new() { return new T(); } The key here is where T : new(). In the general case, using reflection, you can use the following: void T createT() { var typeOfT = typeof(T); return (T) Activator.

CreateInstance(typeOfT, new object { arg1, arg2 … }); }.

– Konrad Rudolph Oct 16 '09 at 14:21 Not a solution. You can't know in advance what arguments an arbitrary type is going to require. – Developer Art Oct 16 '09 at 14:21 3 @Developer Art: sorry, your common sense failed you again.

Nobody is talking about completely arbitrary types, anywhere. You always need to make certain assumptions, nobody’s denying this. Code like the above works, and is actually used quite a lot.

It certainly is “a solution� To a lot of problems. Why else would Microsoft provide the method, if it wouldn’t work?

– Konrad Rudolph Oct 16 '09 at 14:23 @Konrad Rudolph: The question author has not specified that he's only interested in types with parameterless constructors. – Developer Art Oct 16 '09 at 14:26 3 @Developer Art: What about the "new T()" in the question? – Michael Myers?

Oct 16 '09 at 14:44.

In c# you can use the where keyword private class MyClass where T : new() { private void AMethod() { T myVariable = new T(); } }.

You can do this in Java via reflection: public class Example { public static void main(String args) throws Exception { String s = create(String. Class); } // Method that creates a new T public static T create(Class c) throws InstantiationException, IllegalAccessException { return c.newInstance(); } }.

One of the workarounds in Java that's less ugly: Javassist.

To summarize, in java there is no way to instantiate a generic type T. In C#, there are several ways. Am I understanding this correctly?

No, it's also possible in Java, though not using new T();. See my answer. – Jesper Oct 16 '09 at 18:30.

Like Donut said, C# allows you to instantiate arbitrary types at runtime using Activator. CreateInstance which works a bit like Class

Since C#’s Generics are preserved after compilation, C#, unlike Java, allows you to get hold of a Type instance for a given type parameter and thus call CreateInstance or any other constructor. The key here is where T : new().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions