Will the following code compile?
 
internal sealed class SomeGenericClass<T> 
{ 
     public static T SomeMethod() 
     { 
          return new T(); 
     }
}
 
Explanation
It will not compile, because constraint is not specified (there is a call of public constructor without parameters, but not all reference types have it). In order for the code to compile it's required to add a constraint, for example:
 
internal sealed class SomeGenericClass<T> where T : struct {
   public static T SomeMethod() { 
       return new T(); 
   } 
}
 
or
 
internal sealed class SomeGenericClass<T> where T : new() { 
   public static T SomeMethod() { 
       return new T(); 
    } 
} 

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести