C# 枚举、结构和类

发布于 2024-11-17 19:53:05 字数 59 浏览 1 评论 0原文

谁能告诉我 C# 中枚举、结构和类之间的主要区别吗?我通常只使用此类代码,但从未真正看到需要使用其他类?

Could anyone just enlighten me on the main differences in C# between enumerations, structs and classes? I usually just use classes for this type of code but never really seen the need to use the others?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间☆小暴躁 2024-11-24 19:53:05

MSDN 是一个很好的参考:枚举结构

MSDN is a great reference: Class, Enum, Struct.

放飞的风筝 2024-11-24 19:53:05

参考:

C# 中的对象和类简介 - 简介

对象和类简介C# 中的类 - 世界上的类和对象

面向对象编程
程序员编写独立的部分
一个称为类的程序。各班
代表程序的一部分
功能和这些类可以是
组装起来形成一个程序。

在我们的世界里,我们有课程和
这些类的对象。一切
在我们的世界中被认为是
目的。例如,人们是
物体,动物也是物体,
矿物是物体;一切都在
世界是一个物体。容易,对吧?
但是课程呢?

在我们的世界中,我们必须与众不同
我们所生活的物体之间
和。所以我们必须明白,有
是分类(这就是他们的方式
获取名称和概念
类)所有这些对象。为了
例如,我是一个物体,大卫是
也是对象,玛丽亚是另一个对象。
所以我们来自人民阶层(或者
类型)。我有一只叫瑞奇的狗,所以
它是一个物体。我朋友的狗,多比,
也是一个对象,所以它们来自
狗的类别(或类型)。

结构类似于类,但有一些差异。

参考结构

结构是一种值类型。帮助
理解结构体,有帮助
与班级进行比较。尽管
结构体是值类型,类是
参考类型。值类型持有
他们在记忆中的价值
已声明,但引用类型持有
对内存中对象的引用。如果
你复制一个结构体,C# 创建一个新的
对象的副本并分配
将对象复制到单独的
结构实例。但是,如果您复制
一个类,C# 创建一个新的副本
对象的引用并赋值
参考文献的副本
单独的类实例。结构不能
有析构函数,但类可以有
析构函数。另一个区别
结构体和类之间是
结构不能有实现
继承,但类可以...

枚举本质上是唯一的类型,允许您将符号名称分配给整数值。

Refering to :

Introduction to Objects and Classes in C# - Introduction

Introduction to Objects and Classes in C# - World's Classes and Objects

In Object-Oriented Programming
programmers write independent parts of
a program called classes. Each class
represents a part of the program
functionality and these classes can be
assembled to form a program.

In our world we have classes and
objects for those classes. Everything
in our world is considered to be an
object. For example, people are
objects, animals are objects too,
minerals are objects; everything in
the world is an object. Easy, right?
But what about classes?

In our world we have to differentiate
between objects that we are living
with. So we must understand that there
are classifications (this is how they
get the name and the concepts of the
Class) for all of those objects. For
example, I'm an object, David is
object too, Maria is another object.
So we are from a people class (or
type). I have a dog called Ricky so
it's an object. My friend's dog, Doby,
is also an object so they are from a
Dogs class (or type).

A Struct is similiar to class with several differences.

Refering to Structs

A struct is a value type. To help
understand the struct, it's helpful to
make a comparison with classes. While
a struct is a value type, a class is a
reference type. Value types hold
their value in memory where they are
declared, but reference types hold a
reference to an object in memory. If
you copy a struct, C# creates a new
copy of the object and assigns the
copy of the object to a separate
struct instance. However, if you copy
a class, C# creates a new copy of the
reference to the object and assigns
the copy of the reference to the
separate class instance. Structs can't
have destructors, but classes can have
destructors. Another difference
between a struct and class is that a
struct can't have implementation
inheritance, but a class can...

Enumerations are essentially unique types that allow you to assign symbolic names to integral values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文