C# basic knowledge compilation: basic knowledge (11) value type, reference type
黄舟
Release: 2017-02-11 13:25:40
Original
1132 people have browsed it
C# is an object-oriented language. In object-oriented thinking, there are only objects, and everything can be described by classes. So for example, these, int, bool, char, string, double, long, etc. are all classes, then things like 30, 2.5, "test" are all objects of the corresponding class.
It can be seen that they have the ToString() method, so they are objects. When writing code in daily life, in addition to the above-mentioned ones, I have definitely used it to define data types:
This is actually a mechanism of .NET. .NET is a platform. On this platform There are languages like C# and VB. Therefore, .NET defines a series of types that are mapped to different languages. Int32 is int in c#. Such data types are called primitive types. In C#, class objects must be generated using new. This part of the class can be directly represented by constants. Primitive types are defined in the .net Framework under the System namespace. Take a look at the type mapping of primitive types in the C# language.
public class ClassPositiveNumber : ICloneable
{
private int number;
public int Number
{
get
{
return this.number;
}
set
{
if (value <= 0)
{
throw new Exception();
}
this.number = value;
}
}
//引用类型自己可以初始化为null,无需定义初始值
//public readonly static ClassPositiveNumber InitialValue = new ClassPositiveNumber();
public ClassPositiveNumber(int value)
{
if (value <= 0)
{
throw new Exception();
}
this.number = value;
}
public object Clone()
{
return new ClassPositiveNumber(this.number);
}
}
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn