Introduction:
Tomiwong yuanyou posted a post. How many people can get the clear value type and reference type?
Question: "Class, object, String, Int, bool, byte, interface, Delegate, struct, and enum. Which of the above are value types and reference types ?"
There are countless replies, but I found that most people still do not know the truth. A few people have already felt the truth, but have not made it clear. Therefore, I hereby write an article to make it clear.
Seemingly correct answer
If you want to quickly give a general answer, the answer should be as follows:
Value Type: int, bool, byte, struct, Enum
Reference Type: Class, object, String, interface, delegate
But is the problem so simple? Are many of the replies boring?
Core issues
The English understanding in the question is ambiguous.
What should I try to ask? YesThe variables defined and declared with these keywords are value type or reference type.
Let's look at a few representative comments:
======================================
# 4th floor, | xiaotie
Interfaces are different from others.
======================================
#14 floor, | ivony...
In these things:
Object, String, Int, bool, byte
Yes
Class, interface, Delegate, struct, Enum
Not type
It can only be said whether the type defined using these keywords is the reference type or the value type.
======================================
# 18th Floor, | leonsun
I just want to ask a question: what type of class is it? What is the object type?
The definition of nouns in this question is vague. I'm afraid the concept of people in this question is also vague.
Don't mislead the masses. Really ~~~
======================================
Well, there are still a lot of comments. Let's start from scratch to find out where the problem is?
1. differentiate between int type and INT type variables
2. define whether the int mentioned in tomiwong refers to the int type variable or the int keyword?
3. Then we can begin to answer questions.
We usually get used to it. Int Is a value type. The textbook answer should be similar:
Int type variables are value type variables.
Therefore, we can deduce a simplified statement.
Int type is Value Type
Many of you cannot bend over the keywords interface, class, and object. Indeed, these things are not considered at ordinary times. We only say that the object type variable is a reference type variable. Therefore, when we see the class, the first response is that it cannot be regarded as an object, and the class is not an object. This is a daily record. Haha
There is a double standard problem:
Think of everything in the question as its corresponding variable, or as this keyword, or as its representative type.
The Chinese Translation of class is a class. What type is the class? Should be nothing
If they are all considered as keywords, none of these types are
Only when we regard all of them as variables and variables defined and declared using these keywords can we answer questions calmly
Standard answer
C # The type system includes the following categories:
Value Type, reference type, pointer type
Value-type variables store data, while reference-type variables store references to actual data. The reference type is also called an object. The pointer type can only be used in the unsafe mode (to the extreme, the pointer is also a value type ).
By packing and unboxing, you can convert the value type to the reference type, and then convert it back to the value type. The reference type cannot be converted to the value type except for the boxed value type.
The value type can also be null (such as: Int ?), This means they can be stored in non-value states.
The value type consists of two types:
Structure and enumeration
The structure is divided into the following types:
Numeric (numeric) Type
Integer, floating point, decimal
Integer types include sbyte, byte, Char, short, ushort, Int, unit, long, And ulong.
Float Type: float, double
Bool
User-defined structure.
Variables of the reference type, also known as objects, can store references to actual data.
Class, interface, delegate
Built-in reference types: object and string
I did not write the answer.
References
Type (C #) value type (C #) reference type (C #)
we all need to take a look at the C # programming guide