2

In C# we can write

Int a=new int();

char a=new Char();

person p=new person(); 

where person is a Class.
So why can't we write

string s =new string(); 

After all string is a reference type. Then why it's not possible?

Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
  • 1
    There are several string constructors, see https://msdn.microsoft.com/en-us/library/system.string.string(v=vs.110).aspx. There just isn't a constructor without arguments, because you could just use string.Empty anyway. This is not that uncommon and in the framework and elsewhere you often find classes having only constructors with arguments. – ckuri Jul 01 '17 at 13:04

4 Answers4

7

You can not write it because string has not Constructor taking 0 parameters. Instead you can write something like this:

String a = new string(new char[]{});
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
2

int and char are primitive types. In C#, they are really just aliases for the Int32 and Char classes. These classes have constructors with no parameters. In the case of your Person() class, it also has a constructor with no parameters if your code compiles. Strings are a bit different. Here is the list of valid String constructors according to MSDN: https://msdn.microsoft.com/en-us/library/system.string.string(v=vs.110).aspx

  • String(Char*)
  • String(Char*, Int32, Int32)
  • String(Char, Int32)
  • String(Char[])
  • String(Char[], Int32, Int32)
  • String(SByte*)
  • String(SByte*, Int32, Int32)
  • String(SByte*, Int32, Int32, Encoding)

Personally, I generally just use something along the following if I want to explicitly initialize a String without going through the process of using a constructor mentioned above:

String testStr = String.Empty;

Which is really the same as:

String testStr = "";
h0r53
  • 3,034
  • 2
  • 16
  • 25
1

It would be pointless to use the constructor to create a new string based on another existing string - that's why there is no constructor overload that allows this. Just do

string s = "String in a C#";
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
  • useless for Int also but we can do it so then why not in case of string ? –  Jul 01 '17 at 13:00
  • That is because `int`, `char`, .. are value types (`struct`s) which must have a parameterless constructor. – Zdeněk Jelínek Jul 01 '17 at 13:02
  • @AkhilJain Strings are immutable reference types. There's the ldstr IL instruction which allows pushing a new object reference to a string literal, so basically strings are "special" reference types. They are immutable. Read more about types – Roxy'Pro Jul 01 '17 at 13:02
  • @ZdeněkJelínek but Classes are reference type so if we can do this in Class then why not in String !! –  Jul 01 '17 at 13:06
  • @AkhilJain You cannot do this wtih classes in general, you can only do this with classes that have a parameterless constructor. Parameterless class constructor is generated by default if no other constructor is specified. `string` class has multiple constructors none of which is parameterless, therefore it cannot be instantiated with parameterless constructor call. – Zdeněk Jelínek Jul 01 '17 at 13:09
1

When you create a reference type (for example a class), you can choose what constructors it will have (for value types a parameterless constructor is required, so your choice is limited). If you do not specify any, then a default parameterless constructor is created for you implicitly. The C# designer team decided that it is not worth having a parameterless constructor on the String type. That's why you cannot call new string(). That's all there is to it.

You can do the same with your custom class.

public class C
{
    public C(int i)
    {
    }
}

var c = new C(); // invalid
Kapol
  • 6,383
  • 3
  • 21
  • 46