C#类和结构简单介绍

开发 后端
这里介绍C#类和结构,一些轻量级的对象最好使用结构,但数据量大或有复杂处理逻辑对象最好使用类。如:Geoemtry最好使用类,而 Geometry 中点的成员最好使用结构。

C#语言还是比较常见的东西,这里我们主要介绍C#类和结构,包括介绍C#类和结构示例等方面。

C#类和结构的区别

类:
◆类是引用类型在堆上分配,类的实例进行赋值只是复制了引用,都指向同一段实际对象分配的内存
◆类有构造和析构函数
◆类可以继承和被继承

结构:
◆结构是值类型在栈上分配(虽然栈的访问速度比较堆要快,但栈的资源有限放),结构的赋值将分配产生一个新的对象。
◆结构没有构造函数,但可以添加。结构没有析构函数。
◆结构不可以继承自另一个结构或被继承,但和类一样可以继承自接口。

C#类和结构示例:
根据以上比较,我们可以得出一些轻量级的对象***使用结构,但数据量大或有复杂处理逻辑对象***使用类。如:Geoemtry(GIS 里的一个概论,在 OGC 标准里有定义) ***使用类,而 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;  
//结构也可以增加构造函数  
public Point(double X, double Y, double Z)  
{  
this.x = X;  
this.y = Y;  
this.z = Z;  
}  
public double X  
{  
get { return x; }  
set { x = value; }  
}  
public double Y  
{  
get { return x; }  
set { x = value; }  
}  
public double Z  
{  
get { return x; }  
set { x = value; }  
}  
}  
//在此简化了点状Geometry的设计,实际产品中还包含Project(坐标变换)等复杂操作  
class PointGeometry  
{  
private Point value;  
 
public PointGeometry(double X, double Y, double Z)  
{  
value = new Point(X, Y, 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 Main(string[] args)  
{  
Point tmpPoint = new Point(1, 2, 3);  
   
PointGeometry tmpPG1 = new PointGeometry(tmpPoint);  
PointGeometry tmpPG2 = new PointGeometry(tmpPoint);  
tmpPG2.X = 4;  
tmpPG2.Y = 5;  
tmpPG2.Z = 6;  
   
//由于结构是值类型,tmpPG1 和 tmpPG2 的坐标并不一样  
Console.WriteLine(tmpPG1);  
Console.WriteLine(tmpPG2);  
   
//由于类是引用类型,对tmpPG1坐标修改后影响到了tmpPG3  
PointGeometry tmpPG3 = tmpPG1;  
tmpPG1.X = 7;  
tmpPG1.Y = 8;  
tmpPG1.Z = 9;  
Console.WriteLine(tmpPG1);  
Console.WriteLine(tmpPG3);  
   
Console.ReadLine();  
}  
}  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.

【编辑推荐】

  1. C#静态变量简单分析
  2. C# sealed修饰符学习笔记
  3. C# const和static readonly区别浅谈
  4. C#隐含类型局部变量浅析
  5. C#对象初始化器描述
责任编辑:佚名 来源: 博客园
相关推荐

2009-08-06 14:53:41

C# User类

2009-09-02 14:33:57

C#类实现接口

2009-09-07 16:09:19

C#和Java特点

2009-08-07 09:29:22

C#数组C#函数

2009-08-20 18:44:54

C#和ADO.NET

2009-08-18 16:57:24

VB.NET和C#

2009-08-27 16:18:47

C#类C#结构体

2009-08-12 09:41:28

C# Director

2009-09-03 15:57:11

C# SystemMe

2009-08-25 13:38:35

C# Timer组件

2009-08-13 17:36:54

编译C#代码

2009-08-06 18:15:13

C# SQL Serv

2009-08-20 16:25:59

C# 匿名方法

2009-08-07 17:12:07

C# DLL函数

2009-08-03 17:51:43

C#引用类型

2009-09-03 09:40:57

C#创建表单

2009-08-14 16:46:44

C#元数据

2009-09-01 16:19:57

C# new()约束

2009-08-21 17:55:52

C#复合控件

2009-08-10 16:19:37

C#冒泡排序
点赞
收藏

51CTO技术栈公众号