Public T GetMyClass() where T : MyClass, new() { /* is this pointless? */ }?

Sure there's a difference. Let's say you have a second class deriving from MyClass: class MyClass2 : MyClass { } Then you can do MyClass2 myClass2 = confused.GetMyClass() You can't do that with the other function.

Sure there's a difference. Let's say you have a second class deriving from MyClass: class MyClass2 : MyClass { } Then you can do MyClass2 myClass2 = confused.GetMyClass(); You can't do that with the other function.

If you write the generic version, you can instantiate and return derived classes: where T : MyClass Also, with the generic version you don't need the activation code: return new T(); This is because you have specified: where T : new() The generic constraint enforcing a public parameterless constructor.

MyClass could be a base class (or an interface IMyClass). The generic version with the constraint says you want this function to work for any class derived from (or implementing) a common base or interface and to return the result as that derived class, not as the base. Class MyClass { } class MySpecializedClass : MyClass { } // etc.

There is a very big difference: The non generic version can only return instances of type MyClass, whereas the generic version can return instances of type MyClass and all classes derived from MyClass!

No. They are not the same. The first will only ever construct a MyClass object, and the second will construct any object that is a MyClass or a descendent of MyClass, based on the type parameter.

They would give the same result ONLY if you called .GetMyClass(). However, I presume the extra method has been created to allow for creation of other classes? If not, then they're the same, so one's redundant (and I'd get rid of the generic version as has overhead inside the assembly).

Are they being used differently?

They are not the same. The generic allows inherited classes to be built as well like this: class MyClass { public override string ToString() { return "I am confused now"; } } class InheritedClass : MyClass { } class Confused { public MyClass GetMyClass() { return new MyClass(); } public T GetMyClass() where T : MyClass, new() { return System.Activator.CreateInstance(); } } class Program { static void Main() { Confused c = new Confused(); System.Console. WriteLine(c.GetMyClass()); System.Console.

WriteLine(c.GetMyClass()); System.Console. WriteLine(c.GetMyClass()); } }.

I'm not quite sure, but Generics are a runtime feature in .NET. Therefore, the non-generic method isn't equivalent to the generic one, even though it is used equivalently. As they're public, this can't be optimized away.

As far as I know, they aren't (in . NET). See also stackoverflow.Com/questions/6127855/…... – Mudu Jul 14 at 14:37.

They would give the same result ONLY if you called . However, I presume the extra method has been created to allow for creation of other classes? If not, then they're the same, so one's redundant (and I'd get rid of the generic version as has overhead inside the assembly).

Are they being used differently?

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