Value type class definition in C#?

2019-05-31 14:25发布

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

4条回答
太酷不给撩
2楼-- · 2019-05-31 14:55
public struct Example
{
    public int Property {get;set;}
}

Or, what did you have in mind when you asked for an example?

查看更多
时光不老,我们不散
3楼-- · 2019-05-31 15:03

EDIT 2

As it now seems, you're looking to declare a true value type using the class keyword, 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 override Equals() and GetHashCode(). You'll almost certainly want to implement IComparable<T> and IEquatable<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.

查看更多
来,给爷笑一个
4楼-- · 2019-05-31 15:03

It is possible to design a class that mimics immutable value semantics, almost. System.String falls into that category. Given an instance of a class, the only way to have an instance which is like the first but differs in some way is to construct a new instance based upon the original. To achieve "immutable value semantics, almost", a class should override Object.Equals() and Object.GetHashCode() in such a way that two instances which hold identical data will compare equal to each other and yield the same hashcode.

For classes to mimic mutable value semantics, there would have to be a means of specifying that copying a field of that class type should also copy the object referred to thereby (and if that process requires copying any similarly-declared fields from that object, the objects referred to by those fields would have to be copied as well, etc.) There would be ways of adding support for this into the CLR, but at present no such feature exists.

查看更多
不美不萌又怎样
5楼-- · 2019-05-31 15:12

Although ValueType is the implicit base class for value types, you cannot create a class that inherits from ValueType directly. Instead, individual compilers provide a language keyword or construct (such as struct in C# and Structure…End Structure in Visual Basic) to support the creation of value types.

source: http://msdn.microsoft.com/en-us/library/system.valuetype.aspx

查看更多
登录 后发表回答