Re: Basic Question - Array in Struct
Quote:
Originally Posted by Mutant_Fruit
That's a terrible reason to avoid strings ;) C# is a managed language, use strings all you want. Combine them, split them, do whatever you like. Otherwise you'll just end up having to write buggy code which duplicates all the string functions but operates on char[] and has 1/10th of the performance of the built in string class.
Of course, if you're doing a *lot* of string mangling, them a StringBuilder might be what you need, but i definitely would not recommend you use char[] as a 'string'.
I am 100% positive that the designers of C# are much, much, much smarter than I am.
However, having said that, it seems to me that it is the opposite. That is a terrible way to design what is probably the most commonly used C# string class in the first place.
I am not saying there is not a use for an immutable string class. However, I am saying programmers moving to C# (especially C++ programmers) will not expect the "string" class to be immutable. I would imagine that most, like myself, will assume that it is similar to std::string and use it as such. That would be a big mistake and one that I made. (I did have to write code to parse and manipulate free form input - hundreds of time per input and millions of input per hour. string seemed the obvious choice until I learned otherwise)
I did see stringbuilder, but I was disappointed to find that it's functionality was severely limited when compared to the functionality of string.
This is one reason I asked the question if it was a difficult learning curve for C++ programmers to move to C#. I have been led to believe that it would be simple. (Not the fault of C# just the people giving advice). However, it seems to me that the learning curve is going to be very difficult due to the fact that it seems similar given the syntax, but under the covers it is working very differently.
Re: Basic Question - Array in Struct
Kenrus, in practice you'll find the string class works quite well.
There is definitely no reason to use a char array over the string or stringbuilder classes.
Re: Basic Question - Array in Struct
That's what the garbage collector is for. It'll clean up those extra strings and free their memory once they are unused. I'd still advocate the use of strings in your case. Of course, if you understand that a string is immutable, you can be smarter about how you use strings. Rather than calling ToLower on two strings then comparing them for equality, you'd just use:
strA.Equals (strB, StringComparison.IgnoreCase);
I still could not recommend that you use char[]. I'd still call that a really bad move when you're processing a lot of input.
Re: Basic Question - Array in Struct
There's a very good reason why strings are immutable in C#.
In C++ std::string can be passed by reference or by value.
You know by the syntax whether a string is passed by reference or value.
When you have a std::vector<std::string> the strings are copied into the vector. Changing the source string doesn't change the string in the vector i.e.
Code:
std::string example = "hello";
std::vector<std::string> vec;
vec.push_back(example);
example = "there";
// vec[0] = "hello"
You don't have the choice in C# - you either have pass by reference with classes or pass by value with structs.
So since string is a class (if string was a struct and always was passed by value then this would have a terrible impact on performance of any C# application) then it is always passed by reference. If string was not immutable this would cause all sorts of problems in C# applications e.g.
Code:
string example = "hello";
List<string> listExample = new List<string>();
listExample.Add(example);
// any changes to string instance example would change the value in the list if string wasn't immutable
In the 5 years I've been coding in C# I've yet to have a performance problem with string. In fact I've had more performance problems with std::string (and people passing that by value, using large numbers of strings in std::vector<std::string> rather than using a std::list<std::string>) than I've had with C# string.
Darwen.