Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize generic parameter type T?

Tags:

c#

.net

generics

Simple question:
If you have a string x, to initialize it you simple do one of the following:

string x = String.Empty;   

or

string x = null; 

What about Generic parameter T?

I've tried doing:

void someMethod<T>(T y) {     T x = new T();       ... } 

Generate error :
Cannot create an instance of the variable type 'T' because it does not have the new() constraint

like image 924
JavaSa Avatar asked Dec 21 '12 11:12

JavaSa


People also ask

How do you initialize a generic object?

If you want to initialize Generic object, you need to pass Class<T> object to Java which helps Java to create generic object at runtime by using Java Reflection.

Can you instantiate a generic type?

To use Java generics effectively, you must consider the following restrictions: Cannot Instantiate Generic Types with Primitive Types. Cannot Create Instances of Type Parameters.

What is T on parameter C#?

In C#, the “T” parameter is often used to define functions that take any kind of type. They're used to write generic classes and methods that can work with any kind of data, while still maintaining strict type safety.

How do you declare a generic type in Java?

To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class. As you can see, all occurrences of Object are replaced by T.


1 Answers

You have two options:

You can constrain T: you do this by adding: where T : new() to your method. Now you can only use the someMethod with a type that has a parameterless, default constructor (see Constraints on Type Parameters).

Or you use default(T). For a reference type, this will give null. But for example, for an integer value this will give 0 (see default Keyword in Generic Code).

Here is a basic console application that demonstrates the difference:

using System;  namespace Stackoverflow {     class Program     {         public static T SomeNewMethod<T>()             where T : new()         {             return new T();         }          public static T SomeDefaultMethod<T>()             where T : new()         {             return default(T);         }          struct MyStruct { }          class MyClass { }          static void Main(string[] args)         {             RunWithNew();             RunWithDefault();         }          private static void RunWithDefault()         {             MyStruct s = SomeDefaultMethod<MyStruct>();             MyClass c = SomeDefaultMethod<MyClass>();             int i = SomeDefaultMethod<int>();             bool b = SomeDefaultMethod<bool>();              Console.WriteLine("Default");             Output(s, c, i, b);         }          private static void RunWithNew()         {             MyStruct s = SomeNewMethod<MyStruct>();             MyClass c = SomeNewMethod<MyClass>();             int i = SomeNewMethod<int>();             bool b = SomeNewMethod<bool>();              Console.WriteLine("New");             Output(s, c, i, b);         }          private static void Output(MyStruct s, MyClass c, int i, bool b)         {             Console.WriteLine("s: " + s);             Console.WriteLine("c: " + c);             Console.WriteLine("i: " + i);             Console.WriteLine("b: " + b);         }      } } 

It produces the following output:

New s: Stackoverflow.Program+MyStruct c: Stackoverflow.Program+MyClass i: 0 b: False Default s: Stackoverflow.Program+MyStruct c: i: 0 b: False 
like image 135
Wouter de Kort Avatar answered Sep 17 '22 09:09

Wouter de Kort