Emulating behaviour of C# string

P

Paul E Collins

I plan to write a class representing a particular type of string from an
obsolete machine architecture. It involves a non-ASCII character set and
has some rendering quirks such as "characters" that expand to reserved
words when displayed.

If possible, I'd like my string class to behave like a value type
insofar as the standard C# string does. For instance, if someone passes
a MyString instance to another method, and that method changes
properties of the instance, I'd rather not have those changes propagated
back to the caller.

Is this feasible? If so, I'd appreciate any pointers on how to do it,
but I suspect that the C# string (as a built-in part of the language)
can probably take liberties that I can't.

Eq.
 
I

Ignacio Machin \( .NET/ C# MVP \)

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
Paul E Collins said:
I plan to write a class representing a particular type of string from an
obsolete machine architecture. It involves a non-ASCII character set and
has some rendering quirks such as "characters" that expand to reserved
words when displayed.

If possible, I'd like my string class to behave like a value type insofar
as the standard C# string does.

IIRC String is a reference type, what behavior of it makes you think it's a
value type?
For instance, if someone passes a MyString instance to another method, and
that method changes properties of the instance, I'd rather not have those
changes propagated back to the caller.

You are confused regarding the "feature" you talk about. What String
implements (that ANY other class can implement) is the concept of inmutable.
This can be easily implemented by no providng any method that can change the
internal values of the instance. Instead you provide methods that return a
new instance for each "changing" operation your type expose.
 
J

Jon Skeet [C# MVP]

Paul E Collins said:
I plan to write a class representing a particular type of string from an
obsolete machine architecture. It involves a non-ASCII character set and
has some rendering quirks such as "characters" that expand to reserved
words when displayed.

If possible, I'd like my string class to behave like a value type
insofar as the standard C# string does. For instance, if someone passes
a MyString instance to another method, and that method changes
properties of the instance, I'd rather not have those changes propagated
back to the caller.

Is this feasible? If so, I'd appreciate any pointers on how to do it,
but I suspect that the C# string (as a built-in part of the language)
can probably take liberties that I can't.

Well, only in a very few ways. Basically, if you don't provide any
methods which change the internal state of the object, you'll achieve
the same immutability.

As an example of another string-like class, I've got a Utf32String in
my MiscUtil library:

http://pobox.com/~skeet/csharp/miscutil

Feel free to download the source and have a look. (It was a while ago,
so there may be improvements I'd make these days...)
 
P

Paul E Collins

Jon Skeet said:
Basically, if you don't provide any methods which change the internal
state of the object, you'll achieve the same immutability.

Oh, I thought I'd read somewhere that there was more special behaviour
under the hood.

That's simple enough then; I'll just return a new instance from any
instance method that would otherwise be expected to modify the current
one. Thanks.

Eq.
 
J

Jeroen Mostert

Paul said:
I plan to write a class representing a particular type of string from an
obsolete machine architecture. It involves a non-ASCII character set and
has some rendering quirks such as "characters" that expand to reserved
words when displayed.
That's not exactly a string type, it's an encoding. Characters that "expand"
to words aren't characters, they're tokens. "Token string" might also be
appropriate: it's all tokens, and "displaying" it is converting it to a
plain old string.

I'm not trying to nitpick you here, just pointing out a different way of
looking at things.
If possible, I'd like my string class to behave like a value type
insofar as the standard C# string does.

Well, others already answered your actual question. :)
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,



Paul E Collins said:
Oh, I thought I'd read somewhere that there was more special behaviour
under the hood.

The only thing special about string is that the class name is String (with
capital S) but the C# compiler let you use string (with lowercase) as an
alias
 
J

Jon Skeet [C# MVP]

The only thing special about string is that the class name is String (with
capital S) but the C# compiler let you use string (with lowercase) as an
alias

No, there's definitely more than that:

1) The "+" operator is part of C# rather than the runtime
2) String interning is part of the CLI
3) The CLI has a literal format for strings

In other words, strings have direct support in both the language and
the runtime. Compare that with, say, DateTime which has (AFAIK) no
special handling.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top