Is it possible to make a class that is not a struct but is a value type, or is like a value type in that it copies on being passed instead of being passed by reference.
edit:
Sorry about the question having to be edited after being asked.
Also, see this question for more information.
Cycle in the struct layout that doesn't exist
EDIT 2
As it now seems, you’re looking to declare a true value type using the
classkeyword, which is by definition not possible.Since you’re looking at creating a class with semantics similar to System.String, you should probably decompile System.String. A lot of its magic is hidden away in the CLR, but much of what you will see will help.
For starters, you’ll definitely need to overload
==and!=, and overrideEquals()andGetHashCode(). You’ll almost certainly want to implementIComparable<T>andIEquatable<T>as well.Another important aspect of strings is that they are immutable. This is an important part of their value-like behavior, because it guarantees that two equal strings will always be equal. If strings were mutable, it would be possible to modify one of the strings to make it unequal to the other.
I should also point out that while string has semantics that make it seem like a value type, it is of course a reference type, and some aspects of reference semantics are unavoidable.
If you post a little more about why you want to do this, we can offer more specific advice.
EDIT
In response to your edit, it seems you have a misconception about strings. While they do behave like value types in some ways, they are not passed directly by copying their data each time they are passed to a method. The only way to achieve that is to declare a
struct. Strings, like all classes, are reference types that can be accessed only by reference; you can only manipulate the reference directly; you can only pass the reference to a method.