您的位置:首页 > 编程语言 > C#

C# class and struct

2015-08-12 14:05 537 查看
"A class or struct definition is like a
blueprint that specifies what the type can do. An
object is basically a block of memory that has been allocated and configured according to the blueprint. A
program may create many objects of the same class. Objects
are also called instances, and they can be stored in either a named variable or in an array or collection. Client
code is the code that uses these variables to call the methods and access the public properties of the object. In
an object-oriented language such as C#, a typical program consists of multiple objects interacting dynamically."

---from Microsoft Developer Network

Class

"Because classes are reference types, a
variable of a class object holds a reference to the address of the object on the managed heap. If
a second object of the same type is assigned to the first object, then both variables refer to the object at that address. This
point is discussed in more detail later in this topic."

<span>	</span>Country country1 = new Country();
<span>	</span>//new object being created using class
<span>	</span>
<span>	</span>Country country2 =country1;
<span>	</span>// assigning country2 to the same reference as country1
<span>	</span>// so that country 1 and 2 contain the references to the same address, any change made on country2 results in change on country1
</pre><pre name="code" class="csharp">

struct

"Because structs are value types, a variable of a struct object
holds a copy of the entire object"

<span>	</span>Country C1 = new C1();
<span>	</span>
<span>	</span>Country C2 = C1;
<span>	</span>// the whole object of C1 is copied to C2, meaning that there are now two objects(a new object is created)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: