Why does the String class not have a parameterless constructor? - c#

int and object have a parameterless constructor. Why not string?

Because there is no point in doing that.
string is immutable. Creating an empty string is just useless.
MSDN:
Strings are immutable--the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this.
As Jonathan Lonowski pointed out, we have string.Empty for that.

Update:
To provide more information for you.
You don't have an empty Constructor with a string, however you do have String.Empty. The reason is because a string is an immutable object every instance of a string you modify is actually creating a new string in memory.
For instance: string name = ""; though it is an empty string it will still hold around twenty bytes. Where the string.Empty will only hold around four or eight bytes. So though they mean the same thing, one is more efficient than the other.
However I believe you want an empty Constructor to do manipulation that may be more commonly handled by the StringBuilder. Some really nice usage between the two can be found here (Determine performance hit / usage).
Some additional information on the string can be found here. They are immutable thus the contents cannot be changed afterwards.
Example:
string first = "Greg "; // Creates string "first" in memory.
string last = "Arrigotti "; // Creates string "last" in memory.
string name = first + last; // Creates string "name" in memory.
As you edit one of these, it is simply creating a whole new string in memory. If you are looking at a way to potentially handler user data in a field where no middle name exist for instance, the empty string may contain valid usage.
Hopefully these point you in the proper direction.

Strings are immutable, therefore new String() has no purpose. What would you do with it?

As said before, strings are immutable and therefore if you manipulate a string you actually create a new one every time.
Example:
string s = "str"; // str was created in the memory.
s += "2"; // str2 was created in the memory.
Use StringBuilder when you want to manipulate string(that's why you wanted an empty ctor, right?)

Why indeed?
It would be completely logical and sensical to provide a parameterless constructor for the string type, yet it doesn't have one.
The reason is because the designers of that type thought it would be a much better idea to have string.Empty.
There could be a logical reason for having the ability to construct multiple empty strings that are different instances. I fail to see one off the top of my head, but that doesn't mean someone else can't see one.
There are some technical reasons behind why limiting the usage to string.Empty might be a good idea. First, all empty strings are considered equal, though not necessarily ReferenceEquals, so having multiple empty strings would seemingly make no sense. The second you say that "I have these two seemingly similar things, yet I've attached a different meaning to each" then perhaps you're trying to solve a problem with the wrong tool.
There's also some upshots of having a predefined string.Empty. Whenever you reference it, you're referencing the same object instance as every other place, and thus you don't have lots of empty (and identical) string objects in memory.
But could it be done? Sure.
So while everybody here has tried to justify that there should be no such constructor, I am saying that there could be such a constructor.
However, someone decided to design the type without one.

Also there is already a defined constant for this: String.Empty

int is a value type, and as such it must have a parameterless constructor. There is no consideration that can be made here.
object has no reason to have anything but a parameterless constructor. There is no data to give it. What parameters would you expect it to take? objects constructed with a parameterless constructor also have a purpose; they are used, for example, as objects to lock on. It is however a class, so it doesn't need to have a public parameterless constructor, however since it has no need for parameters, it's a question of whether you want instance of it to be constructed at all; Microsoft chose to make it concrete, rather than abstract.
string is a class, so it isn't required to have a parameterless constructor. The team building it simply never saw a need to have one. One could sensibly use such a constructor to create an empty string, but they choose to expose string.Empty (as well as an empty string literal) as a way of explicitly creating an empty string. Those options have improved clarity over a parameterless constructor.
Another pretty significant advantage of string.Empty and the empty literal string is that they are capable of re-using the same string instance. Since strings are immutable, the only way to observe the difference between two different references to empty strings is through the use of ReferenceEquals (or a lock on the instance). Because there is virtually never a need to go out of your way to have different references to an empty string, removing the parameterless constructor removes the possibility of an equivalent but poorer performing method of constructing an empty string. In the very unlikely event that it is important to construct a new string instance that is an empty string, an empty char array can be passed to the relevant constructor overload, so removing the parameterless constructor doesn't remove any functionality from the end user; it simply forces you to go out of your way to do something really unusual if you want to do something really unusual, which is the sign of good language design.

Provided that you know that string is immuable, your question can be rephrased as the following:
why on earth can't I initiate a null object??
answer:
Because there is no null object :)

Related

What is the difference between string.Empty and null in memory

I understand the difference of assigning a value or not, what I would like to understand is how the assignment is handle in memory.
What will be stored in the HEAP and in the STACK? Which one is the most efficient?
For example is more efficient to have a method signature like
private Item GetItem(pageModel page, string clickableText = null);
Or
private Item GetItem(pageModel page, string clickableText = "");
Note:
The question is not about which one to use. It is about how their differ in memory.
The proposed method might be called a few hundred times - therefore a different variable assignment might/could have an impact?
There's no difference. The compiler interns string literals, so you're not creating a new string with the call, just referencing an existing string.
The heap and the stack are implementation details in C#. There is some behaviour that depends on the runtime, but the only real contract is that the runtime provides as much memory as you ask for, and guarantees the memory is still there if you access it in the future.
If you do care about the implementation details of the current desktop .NET runtimes, reference types are never passed on the stack. String is a reference type, so it is always passed by reference, and never by value. However, arguments aren't even required to be on the stack in the first place - the reference can also be passed in a register.
In general, in a managed language like C#, you should only care about what exactly happens in memory if you have a good reason it affects the characteristics of your program. The default case should always be thinking about the semantics. Should an empty string mean "no value"? Should a null string mean "no value"? That depends on the semantics of your program. Until you have a good reason to believe the decision is e.g. performance critical, just go with the most clear option, least prone to mistakes, and easiest to read and modify.
A null string is a string that has not been initialized. It is a string variable that hasn't even been given some memory to store data. This will create a null string:
string myString; //Without initializing it, will create a null string.
An empty string is a string that has been initialized and given some memory, but it just doesn't contain any characters (except a null terminator at the end, but you don't see that) so as far as the compiler and you are concerned, it is a string with a length of 0.
string myString = String.Empty; //Will create an empty string.
In terms of efficiency, there shouldn't be a difference at all, but it would good to keep in mind that NULL's can cause projects to crash more than empty strings, unless you are using the NULL pattern in your code.
We have four main types of things we'll be putting in the Stack and Heap as our code is executing: Value Types, Reference Types, Pointers, and Instructions.
Rules
A Reference Type always goes on the Heap.
Value Types and Pointers always go where they were declared. This is a little more complex and needs a bit more understanding of how the Stack works to figure out where "things" are declared.
The Stack, as we mentioned earlier, is responsible for keeping track of where each thread is during the execution of our code (or what's been called).
You can think of it as a thread "state" and each thread has its own stack. When our code makes a call to execute a method the thread starts executing the instructions that have been JIT compiled and and live on the method table, it also puts the method's parameters on the thread stack. Then, as we go through the code and run into variables within the method they are placed on top of the stack.
String.Empty and "" are almost the same, both refer to an existing string that has no content.
Said almost because, "" creates a temporary string in memory (to have something to compare against) while String.Empty is a language constant.
On the other hand, null means nothing, no object at all.
In more familiar terms, String.Empty is like having an empty drawer while null means no drawer at all!

What's the practical difference between a variable being mutable vs non-mutable

I'm just learning C# and working with some examples of strings and StringBuilder. From my reading, I understand that if I do this:
string greeting = "Hello";
greeting += " my good friends";
that I get a new string called greeting with the concatenated value. I understand that the run-time(or compiler, or whatever) is actually getting rid of the reference to the original string greeting and replacing it with a new concatenated one of the same name.
I was just wondering what practical application/ramification this has. Why does it matter to me how C# shuffles strings around in the background when the effect to me is simply that my initial variable changed value.
I was wondering if someone could give me a scenario where a programmer would need to know the difference. * a simple example would be nice, as I'm a relative beginner to this.
Thanks in advance..
Strings, again, are a good example. A very common error is:
string greeting = "Hello Foo!";
greeting.Replace("Foo", "World");
Instead of the proper:
string greeting = "Hello Foo!";
greeting = greeting.Replace("Foo", "World");
Unless you knew that string was an immutable class, you could suspect the first method would be appropriate.
Why does it matter to me how C# shuffles strings around in the background when the effect to me is simply that my initial variable changed value.
The other major place where this has huge advantages is when concurrency is introduced. Immutable types are much easier to deal with in a concurrent situation, as you don't have to worry about whether another thread is modifying the same value within the same reference. Using an immutable type often allows you to avoid the potentially significant cost of synchronization (ie: locking).
I understand that the run-time(or compiler, or whatever) is actually getting rid of the reference to the original string greeting and replacing it with a new concatenated one of the same name.
Pedantic intro: No. Objects do not have names -- variables do. It is storing a new object in the same variable. Thus, the name (variable) used to access the object is the same, even though it (the variable) now refers to another object. An object may also be stored in multiple variables and have multiple "names" at the same time or it might not be accessible directly by any variable.
The other parts of the question have already been succinctly answered for the case of strings -- however, the mutable/immutable ramifications are much larger. Here are some questions which may widen the scope of the issue in context.
What happens if you set a property of an object passed into a method? (There are these pesky "value-types" in C#, so it depends...)
What happens if a sequence of actions leaves an object in an inconsistent state? (E.g. property A was set and an error occurred before property B was set?)
What happens if multiple parts of code expect to be modifying the same object, but are not because the object was cloned/duplicated somewhere?
What happens if multiple parts of code do not expect the object to be modified elsewhere, but it is? (This applies in both threading and non-threading situations)
In general, the contract of an object (API and usage patterns/scope/limitations) must be known and correctly adhered to in order to ensure program validity. I generally find that immutable objects make life easier (as then only one of the above "issues" -- a meager 25% -- even applies).
Happy coding.
C# isn't doing any "shuffling", you are! Your statement assigns a new value to the variable, the referenced object itself did not change, you just dropped the reference.
The major reason immutability is useful is this:
String greeting = "Hello";
// who knows what foo does
foo(greeting);
// always prints "Hello" since String is immutable
System.Console.WriteLine(greeting);
You can share references to immutable objects without worrying about other code changing the object--it can't happen. Therefore immutable objects are easier to reason about.
Most of the time, very little effect. However, in the situation of concatenating many strings, the performance hit of garbage collecting all those strings becomes problematic. Do too many string manipulations with just a string, and the performance of your application can take a nosedive.
This is the reason why StringBuilder is more effective when you have a lot of string manipulation to do; leaving all those 'orphaned' strings out there makes a bigger problem for the Garbage Collector than simply modifying an in memory buffer.
I think the main benefit of immutable strings lies in make memory management easier.
C# allocates memory byte by byte for each object. If you create a string "Tom" it takes up three bytes. You may then allocate an integer and that would be four bytes. If you then tried to change the string "Tom" to "Tomas" it would require moving all the other memory to make room for the two new characters a and s.
To eliminate this pain, it's easier (and quicker) to just allocate five new bytes for the string "Tomas".
Does that help?
In performance terms, the advantage of immutuable is copying an object is cheap in terms of both CPU and memory since it only involves making a copy of a pointer. The downside is that writing to the object becomes more expensive since it must make a copy of the object in the process.

Which is the best practice to use in string in ASP.NET(C#)

I want to know which is the preferred way to declare a string in C#:
string strEmpty = String.Empty;
or
string strNull = null;
Which practice is better or what is the difference between these statements.
the first answer makes the value of the string an actual empty string. Assigning it to null makes the pointer point to nothing. This means that if you tried to do strEmpty.Function(), it wouldn't work in the second case.
The first takes more memory initially, but is more clear.
The correct answer depends on what you would do next. If you are just going to reassign the string, I would make it null. If you intend to do stuff to the string (execute functions, append, etc), I would make it string.empty.
The difference is semantical.
Whenever you find in the code a NULL pointer, it means that is truly nothing. It means it has not been initialized to anything, so you can't use it.
On the other hand, an empty string is a string. It's been initialized and any programmer looking at the code will consider that string as a valid object to be used.
Consider for example having a relative URL stored in a string. If you find that string empty, you'll think it's pointing to the root path. But if it's NULL, you should consider it's not a valid variable whatsoever - it's not been initialized and you should not use it.
Based on that, you'll take different paths - an empty URL means something very different from a NULL variable.
There is no "best". What you do depends on what you mean.
An empty string has a length of 0, but it is a string. Do this when you absolutely must return some kind of string, even if it's zero length. This is rare.
A null is not a string in the first place, it's a null pointer. Do this when the absence of a string is a meaningful condition that may influence other parts of the application.
In a typical line-of-business app, the semantic difference is very clear: Null means the value is unknown or not applicable, and Empty means the value is known to be blank.
In other types of app, the semantics are more or less up to you, as long as you document them and apply them consistently across your whole team.
There is no difference in performance.
The difference between null and empty string is indeed semantic but they are largely equivalent by convention.
In .NET the System.String class has a static method IsNullOrEmpty that it is best practice to use in most cases.

Does it make sense to copy an immutable type?

Does it make sense to implement a copy method on an immutable type, returning a new instance? Or should it just be the current instance?
I thought the type doesn't change anyway so why copy? Like no one copies the number 5, right?
There are certain cases where it makes sense. Java strings are a good example. When a string is created in Java, it has a reference to a backing array of characters (a char[]). It knows the offset into the char array, and the length of the string. When you create a substring, that refers to the same backing array. Now consider this code:
String x = buildVeryLongString();
String y = x.substring(0, 5);
// Use y a lot, but x is garbage collected
The fact that y is still in the system means that the original char[] used by x is still required. In other words, you're using more memory than you have to. If you change the code to:
String x = buildVeryLongString();
String y = new String(x.substring(0, 5));
then you'll end up copying the data to a new char[]. When x and y have rougly the same lifetimes this approach wastes memory (by having two copies) but in the case where x is garbage collected before y, it can make a big difference.
I've run into a similar example with strings in real life, when reading words from a dictionary. By default, BufferedReader.readLine() will use a buffer of 80 characters for a line to start with - so any (non-empty) string returned by readLine() will refer to a char[] array of at least 80 characters. If you're reading a dictionary file with one word per line, that's a lot of wasted space!
This is just an example, but it shows the difference between two immutable objects which are semantically equivalent in terms of what you do with them, but have different characteristics in other ways. That is usually at the heart of why you'd want to copy an immutable type - but it's still a pretty rare thing to want to do.
In .NET, strings are stored somewhat differently - the character data is held within the string object itself instead of in a separate array. (Arrays, strings and IntPtr are the only variable-size types in .NET, as far as I'm aware.) However, the "buffer" in the string can still be larger than it needs to be. For example:
StringBuilder builder = new StringBuilder(10000);
builder.Append("not a lot");
string x = builder.ToString();
The string object referred to by x will have a huge buffer. Changing the last line to builder.ToString().Copy() would make the large buffer eligible for garbage collection immediately, leaving a small string instead. Again, doing this unconditionally is a bad idea, but it can be helpful in some cases.
Technically, an integer is a value type so it is copied constantly. :)
That said, making a copy of an immutable object doesn't make sense. The examples of strings provided by others seems to be a band aid on abstraction leakage by those classes.
I'll assume we mean objects (classes), since it is a moot point for structs.
There are a few dubious reasons for cloning an immutable object:
if the object is remoted, and you want a local copy (although in this case you presumably couldn't use an instance method on the object itself, as that would also return a remoted instance - you'd have to have the clone method local (non-remoted))
if you are hugely worried about reflection (even readonly fields can be changed by reflection) - perhaps for some super-security conscious code
if some external API (that you can't control) uses reference equality, and you want to use the same "value" as two separate keys - OK, I'm stretching things now...
If we extend the discussion to consider deep cloning, then it becomes more reasonable, since a regular immutable object doesn't imply that any associated objects are also immutable. A deep clone would fix this, but is a separate consideration.
I think maybe the remoting scenario is the best I can do...
Well Java's String class has this:
String(String original)
Initializes a newly created String object so that it represents the same
sequence of characters as the argument; in other words, the newly created
string is a copy of the argument string.
and .Net's has the Copy() method which does the same.
Both frameworks were designed by people who are smarter than I am, so there must be a good reason - someone at sometime needs strings that are different reference-wise, but have the same value.
I'm just not sure when that would be...
Does it make sense to provide a 'Copy'
operation on an immutable object?
No.
(There is lots of other interesting discussion in the other answers, but I thought I'd provide the short answer.)
If said object needs to implement an interface that requires a Clone() method (or moral equivalent), it is fine to 'return this'.
One of the advantages of immutable types is that they can be interned (e.g., Java strings). Certainly you shouldn't make extra copies if you can avoid it.

Are "proxy properties" good style?

I have a class with a string property that's actually several strings joined with a separator.
I'm wondering if it is good form to have a proxy property like this:
public string ActualProperty
{
get { return actualProperty; }
set { actualProperty = value; }
}
public string[] IndividualStrings
{
get { return ActualProperty.Split(.....); }
set
{
// join strings from array in propval .... ;
ActualProperty = propval;
}
}
Is there any risks I have overlooked?
Linking two settable properties together is bad juju in my opinion. Switch to using explicit get / set methods instead of a property if this is really what you want. Code which has non-obvious side-effects will almost always bite you later on. Keep things simple and straightforward as much as possible.
Also, if you have a property which is a formatted string containing sub-strings, it looks like what you really want is a separate struct / class for that property rather than misusing a primitive type.
Seems that the array is the real data, and the single-string stuff is a convenience. That's fine, but I'd say look out for things like serialization and memberwise cloning, which will get and set both writeable properties.
I think I would;
keep the array as a property
provide a GetJoinedString(string seperator) method.
provide a SetStrings(string joined, string seperator) or Parse(string joined, string seperator) method.
Realistically, the seperator in the strings isn't really part of the class, but an ephemeral detail. Make references to it explicit, so that, say, a CSV application can pass a comma, where a tab-delimited app could pass a tab. It'll make your app easier to maintain. Also, it removes that nasty problem of having two getters and setters for the same actual data.
Define "good". It shouldn't break (unless you failed to properly guarantee that the delimiter(s) passed to Split() are never allowed in the individual strings themselves), but if IndividualStrings is accessed more frequently than ActualProperty you'll end up parsing actualProperty far more often than you should. Of course, if the reverse is true, then you're doing well... and if both are called so often that any unnecessary parsing or concatenation is unacceptable, then just store both and re-parse when the value changes.
Properties are intended to be very simple members of a class; getting or setting the value of a property should be considered a trivial operation without significant side-effects.
If setting a property causes public values of the class other than the assigned property to change, this is more significant than a basic assignment and is probably no longer a good fit for the property.
A "complex" property is dangerous, because it breaks from the expectations of callers. Properties are interpreted as fields (with side-effects), but as fields you expect to be able to assign a value and then later retrieve that value. In this way, a caller should expect to be able to assign to multiple properties and retrieve their values again later.
In your example, I can't assign a value to both properties and retrieve them; one value will affect the other. This breaks a fundamental expectation of the property. If you create a method to assign values to both properties at the same time and make both properties read-only, it becomes much easier to understand where the values are set.
Additionally, as an aside:
It's generally considered bad practise to return a temporary array from a property. Arrays may be immutable, but their contents are not. This implies you can change a value within the array which will persist with the object.
For example:
YourClass i = new YourClass();
i.IndividualStrings[0] = "Hello temporary array!";
This code looks like it's changing a value in the IndividualStrings property, but actually the array is created by the property and is not assigned anywhere, so the array and the change will fall out of scope immediately.
public string ActualProperty { get; set; }
public string[] GetIndividualStrings()
{
return ActualProperty.Split(.....);
}
public void SetFromIndividualStrings(string[] values)
{
// join strings from array .... ;
}
Well I'd say your "set" is high risk, what if somebody didn't know they had to pass an already joined sequence of values or your example above was maybe missing that. What if the string already contained the separator - you'd break.
I'm sure performance isn't great depending on how often this property is used.
I'm not sure what the benefit of this design would be. I think the split would be better served in an extension method.
At a minimum, I'd remove the setter on the IndividualStrings property, or move it into two methods: string[] SplitActualProperty() and void MergeToActualProperty(string[] parts).

Categories