Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9069737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:30:30+00:00 2026-06-16T17:30:30+00:00

Simple question: If you have a string x , to initialize it you simple

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:30:31+00:00Added an answer on June 16, 2026 at 5:30 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple regex question. I have a string on the following format: this is a
simple problem with a simple question. I have a string, in C# i can
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
The question is simple. I have a string that contains multiple elements which are
Simple question on best practice. Say I have: public class Product { public string
I have a simple question. I have a long std::string that I want to
I have simple type Question : public class Question { public string[] Tags {
Question is simple: I have two List List<String> columnsOld = DBUtils.GetColumns(db, TableName); List<String> columnsNew
Simple question: I have the following markup... <a href='#'> <img src='icon.png'> This is the
Simple question: If I have a string and I want to add to it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.