C#类和结构 (C# class and structure)

In C# and other programming languages, structure and classes are used to define a custom data type, that we can organize according to our need with different types of variables, methods etc.

在C#和其他编程语言中, 结构和类用于定义自定义数据类型,我们可以根据需要使用不同类型的变量,方法等进行组织。

Both are not the same. Here, we are writing differences between structure and class, they have the following basic differences...

两者不一样。 在这里,我们正在写结构和类之间的差异 ,它们具有以下基本差异...

C#类和C#结构之间的差异 (Differences between C# classes and C# Structures )

  1. Classes are references types of data type, structures are value type of data type.

    类是数据类型的引用类型,结构是数据类型的值类型。

  2. Classes support default constructor i.e. we can set default values that will be assigned while creating an object. Structures do not support the concept of the default constructor, we cannot set values like classes that can be used as default values while creating a structure object/variable.

    类支持默认构造函数,即我们可以设置将在创建对象时分配的默认值。 结构不支持默认构造函数的概念,我们无法在创建结构对象/变量时设置诸如类之类的可以用作默认值的值。

  3. Classes support the inheritance; structures do not support the inheritance.

    类支持继承; 结构不支持继承。

Example:

例:

In this example, we are creating a structure student_1 and a class student_2 along with the methods. To understand the difference between a class and structure in C#, please practice the given example.

在此示例中,我们将创建方法Student_1和class Student_2以及方法。 要了解C#中的类和结构之间的区别,请练习给出的示例。

using System;
using System.Text;
namespace Test
{//structure
public struct student_1{private string name;
private short age;
private float perc;
//method
public void setValue(string name, short age, float perc)
{this.name = name;
this.age = age;
this.perc = perc;
}
public void dispValues()
{Console.WriteLine("Name: {0}", name);
Console.WriteLine("age: {0}", age);
Console.WriteLine("perc: {0}", perc);
}
};
//class
public class student_2{private string name;
private short age;
private float perc;
//default constructor
public student_2()
{this.name = "N/A";
age = 0;
perc = 0.0f;
}
//method
public void setValue(string name, short age, float perc)
{this.name = name;
this.age = age;
this.perc = perc;
}
public void dispValues()
{Console.WriteLine("Name: {0}", name);
Console.WriteLine("age: {0}", age);
Console.WriteLine("perc: {0}", perc);
}
};
class Program
{static void Main(string[] args)
{//creating structure variable
student_1 std1 = new student_1();
//printing default values
Console.WriteLine("std1 (default values)...");
std1.dispValues();
//setting values
std1.setValue("Amit", 21, 98.23f);
//printing after setting the values
Console.WriteLine("std1 (after setting values)...");
std1.dispValues();
Console.WriteLine();
//creating class object
student_2 std2 = new student_2();
//defaut constructor will be invoked
//printing values which we set in default constructor
Console.WriteLine("std2 (default values)...");
std2.dispValues();
//setting values
std2.setValue("Amit", 21, 98.23f);
//printing after setting the values
Console.WriteLine("std2 (after setting values)...");
std2.dispValues();
//hit ENTER to exit
Console.ReadLine();
}
}
}

Output

输出量

std1 (default values)...
Name:
age: 0
perc: 0
std1 (after setting values)...
Name: Amit
age: 21
perc: 98.23
std2 (default values)...
Name: N/A
age: 0
perc: 0
std2 (after setting values)...
Name: Amit
age: 21
perc: 98.23

翻译自: https://www.includehelp.com/dot-net/structure-and-class-differences-in-c-sharp.aspx

C#中的结构和类之间的区别相关推荐

  1. c语言标量变量是什么,C语言中的结构和联合之间的区别

    C中的结构 结构是C语言中可用的用户定义数据类型, 它允许组合不同种类的数据项.结构用于表示记录. 定义结构: 要定义结构, 你必须使用struct声明. struct语句定义一种新的数据类型, 该数 ...

  2. Spring MVC和REST中@RestController和@Controller注释之间的区别

    Spring MVC中的@RestController注释不过是@Controller和@ResponseBody注释的组合. 它已添加到Spring 4.0中,以简化在Spring框架中RESTfu ...

  3. SQL 中的=,in,like之间的区别

    SQL中的=,in,like之间的区别: 三者都可以用来进行数据匹配 .但三者并不相同. 等号是用来查找与单个值匹配的所有数据: IN 是 用来查找与多个值匹配的所有数据: 而 LIKE用来查找与一个 ...

  4. mysql insert into values select_mysql中insert语句中,value与values之间的区别?

    你的位置: 问答吧 -> JAVA -> 问题详情 mysql中insert语句中,value与values之间的区别? mysql> select * from tt; +---- ...

  5. c语言中的typedef struct相当于java的一个类?,C ++中'struct'和'typedef struct'之间的区别?...

    在C ++中,之间有什么区别: struct Foo { ... }; 和 typedef struct { ... } Foo; #1楼 您不能对typedef结构使用forward声明. stru ...

  6. CSS中id选择器和类选择器的区别

    id选择器和类选择器的区别 (1)类选择器(class)好比人的名字,一个人可以有多个名字,同时一个名字也可以被多个人使用. (2)id选择器好比人的身份证号码,全中国是唯一的,不得重复. (3)id ...

  7. Concurrent包下的常用并发类和普通类之间的区别

    1. ConcurrentHashMap和HashMap以及Hashtable之间的区别 1.1 HashMap不是线程安全的,key和value都可为null:而Hashtable是线程安全的,代码 ...

  8. Spring MVC中@RequestParam和@PathVariable批注之间的区别?

    Spring MVC框架是在Java世界中开发Web应用程序最流行的框架之一,它还提供了一些有用的注释,可以从传入的请求中提取数据并将请求映射到控制器,例如@ RequestMapping,@ Req ...

  9. 微内核和宏内核的区别_8086微处理器中的过程和宏之间的区别

    微内核和宏内核的区别 Prerequisite 先决条件 Procedure in 8086 Microprocessor 8086微处理器中的过程 Macros in 8086 Microproce ...

最新文章

  1. s5pv210 uboot-2012-10移植(七) 之支持SD卡
  2. python中global 和 nonlocal 的作用域
  3. Oracle 左连接、右连接、全外连接、(+)号作用
  4. java kiwi_[转] Java 8 开发的 4 大顶级技巧
  5. Java 类型, Hibernate 映射类型及 SQL 类型之间的相应关系
  6. 36氪专访| 友盟+CEO朋新宇:大数据赛道会越来越宽,同时也会越来越头部化
  7. 某宝双十一自动养猫,解放你的双手得喵币
  8. Python报错:local variable referenced before assignment
  9. Java对List集合中的对象的某个中文字段按照拼音首字母进行排序
  10. RTX 4080、RTX4070 Ti 相当于什么水平
  11. 第一步:搭建项目基本框架
  12. 反编译class文件
  13. 车用永磁电机的各种弱磁策略
  14. c语言怎样画坐标轴,c语言 用小星星画各种图形(菜鸟学C语言)
  15. PS对图片进行透明化处理以及字体颜色的加深
  16. Java多线程面试题之如何让主线程等子线程执行完之后再执行
  17. vercel部署hexo主题显示黑色空白
  18. asp毕业设计——基于asp+sqlserver的工厂设备管理系统设计与实现(毕业论文+程序源码)——工厂设备管理系统
  19. 计算机绘图培训心得,几何画板培训心得3篇材料(全文完整版)
  20. css3实现平移效果(transfrom:translate)--冯浩的博客

热门文章

  1. dnslog盲注原理
  2. Metasploit--后渗透(一些基本操作方法)
  3. stream 过滤俩个字段_Java8 Stream:2万字20个实例,玩转集合的筛选、归约、分组、聚合...
  4. MVC 之var与dynamic
  5. js求渐升数的第100位
  6. Ajax jquery的库的简化版本
  7. localStorage/cookie 用法分析与简单封装
  8. C#锐利体验-第八讲 索引器与操作符重载(转)
  9. bzoj3589 动态树 求链并 容斥
  10. C#中用WebClient.UploadData 方法上载文件数据