The difference between class and structure?

This series of articles navigation

The difference between static and non-static members?

const and static readonly difference?

extern What does it mean?

abstract What does it mean?

internal modifier what role?

The sealed modifier is doing?

Override and overload the difference?

What is the index indicator?

The new modifier is what role?

Meaning this keyword?

You can use the function to rewrite the abstract base class virtual function?

C # class based on the concept of the seal, access attributes, interface

The difference between class and structure?

C # based on the concept of abstract classes, interfaces, interfaces, multiple inheritance

What indicator is an alias?

How to manually release resources?

C # based on the concept of P / Invoke, StringBuilder and String

explicit and implicit meanings?

params what's the use?

What is reflection?

16. The difference between Class and structure?
answer:

Class:
class assignment is a reference type instances allocated on the heap in, like just copied reference point to the actual object allocated memory section of the same
class has a constructor and destructor
class can be inherited and inheritance

structure:
the structure is a value type allocated on the stack (although heap stack access speed is faster, but the stack of limited resources put), the structure of the assignment will be assigned to produce a new object.
No constructor structure, but it may be added. Structure no destructor
structure not inherited from another configuration or inheritance, but can be inherited from the same classes, and interfaces

Example:
Based on the above comparison, we can draw some objects lightweight structure is preferably used, but the amount of data complex processing or logical object class is preferably used.
Such as: Geoemtry (a GIS in the introduction, is defined in the standard OGC) type is preferably used, and the members of the structure is preferably used midpoint Geometry


using System;
using System.Collections.Generic;
using System.Text;

namespace Example16
{
interface IPoint
{
double X
{
get;
set;
}
double Y
{
get;
set;
}
double Z
{
get;
set;
}
}
//结构也可以从接口继承
struct Point: IPoint
{
private double x, y, z;
// structure may also increase the constructors public Point ( Double X-, Double the Y, Double the Z) { the this .x = X-; the this .y = the Y; the this .Z = the Z; } public Double X- { GET { return X;} SET {X = value;} } public Double the Y { GET { return X;} SET {X =














value;}
}
public Double the Z { GET { return X;} SET {X = value;} } } // at this point Geometry simplifies the design of an actual product further comprises the Project (coordinate transformation), and other complicated operations class PointGeometry { Private Point value; public PointGeometry ( Double X-, Double the Y, Double the Z) { value = new new Point (X-, the Y, the Z); } public PointGeometry (Point value) {
















//结构的赋值将分配新的内存
this.value = value;
}
public double X
{
get { return value.X; }
set { this.value.X = value; }
}
public double Y
{
get { return value.Y; }
set { this.value.Y = value; }
}
public double Z
{
get { return value.Z; }
set { this.value.Z = value; }
}
public static PointGeometry operator +(PointGeometry Left, PointGeometry Rigth)
{
return new PointGeometry(Left.X + Rigth.X, Left.Y + Rigth.Y, Left.Z + Rigth.Z);
}
public override string ToString()
{
return string.Format("X: {0}, Y: {1}, Z: {2}", value.X, value.Y, value.Z);
}
}
class Program
{
Static void the Main ( String [] args) { Point tmpPoint = new new Point ( . 1 , 2 , . 3 ); PointGeometry tmpPG1 = new new PointGeometry (tmpPoint); PointGeometry tmpPG2 = new new PointGeometry (tmpPoint); tmpPG2.X = . 4 ; tmpPG2. the Y = . 5 ; tmpPG2.Z = . 6 ; // Since the structure is a value type, and the coordinates tmpPG1 not the same tmpPG2










Console.WriteLine (tmpPG1);
Console.WriteLine (tmpPG2);

// Since the class is a reference type, modified coordinates of tmpPG1 affected tmpPG3 PointGeometry tmpPG3 = tmpPG1; tmpPG1.X = . 7 ; tmpPG1.Y = . 8 ; tmpPG1.Z = . 9 ; Console.WriteLine (tmpPG1); Console.WriteLine (tmpPG3); Console.ReadLine (); } } }










结果:
X: 1, Y 2, Z 3
X 4, Y 5, Z 6
X 7, Y 8, Z 9
X 7, Y 8, Z 9

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2011/04/23/2025797.html

Guess you like

Origin blog.csdn.net/weixin_34138377/article/details/93496077