C#中的结构和类之间的区别

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379419.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[转载]SQL Plus 一些使用技巧

原文地址:SQL Plus 一些使用技巧作者:☆水『若寒Sql*plus的使用 Sql*plus介绍 Sql*plus是oracle提供的一个工具程序,既可以在oracle服务器使用,也可以在oracle客户端使用。在windows下分两种,sqlplus.exe是命令行程序&…

云服务器(Centos)部署SVN

1,安装svn yum install subversion 2,查看版本号 svnserve --version 3,创建SVN版本库(在var/svn 文件夹下) 新建文件夹 mkdir -p /var/svn/svnrepos 创建版本库 svnadmin create /var/svn/svnrepos 4,修改…

ffmpeg命令提取像素格式

1: 提取yuv格式:不修改宽高 取3秒 ffmpeg -i test_1920x1080.mp4 -t 3 yuv420p_orig.yuv ffmpeg -i test_1920x1080.mp4 -t 3 -pix_fmt yuv420p yuv420p_orig.yuv 可以使用ffplay播放:ffplay -video_size 1920x1080 yuv420p_orig.yuv 提取y…

Javascript(js)使用function定义构造函数

Javascript并不像Java、C#等语言那样支持真正的类。但是在js中可以定义伪类。做到这一点的工具就是构造函数和原型对象。首先介绍js中的构造函数。 Javascript中创建对象的语法是在new运算符的后面跟着一个函数的调用。如 1 varobj newObject();2 vardate newDate();运算符new首…

错误:将字符串分配给C中的char变量| 常见的C程序错误

If you assign a string to the character variable, it may cause a warning or error (in some of the compilers) or segmentation fault error occurs. 如果将字符串分配给字符变量,则可能会导致警告或错误(在某些编译器中)或发生分段错误。 Consider the code…

【转】用BibTeX 写 Reference

BibTeX 是一种格式和一个程序, 用于协调LaTeX的参考文献处理,BibTeX 使用数据库的的方式来管理参考文献.,BibTeX 文件的后缀名为 .bib。 例子: article{name1, author {作者, 多个作者用 and 连接}, title {标题}, journal {期…

计算机二级C语言易混淆的区别

1,if(a1)与if(a1)的区别 首先,if(a1) 等价于 a1;if(a); 而a 1,是判断a是不是为1; if(sq)里面的分为两种情况,一种是sq为0,不执行if里面的代码内容;另一种是sq不为0,执行里面的代码内…

ffmpeg命令mp3中提取pcm格式

原mp3文件: ffmpeg -i buweishui.mp3 -ar 48000 -ac 2 -f s16le 48000_2_s16le.pcm (这可能是pcm原格式查不到什么信息但是可以播放的:ffplay -ar 48000 -ac 2 -f s16le 48000_2_s16le.pcm) ffmpeg -i buweishui.mp3 -ar 48000 -ac 2 -samp…

C++ STL map的使用

1、map简介 map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。 2、map的功能 自动建立Key - value的…

bfs广度优先搜索算法_图的广度优先搜索(BFS)

bfs广度优先搜索算法What you will learn? 您将学到什么? How to implement Breath first search of a graph? 如何实现图的呼吸优先搜索? Breadth First Search is a level-wise vertex traversal process. Like a tree all the graphs have verte…

考研C++必刷题(一)

【程序1】 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 解题思路: 利用三层循环,分别控制百位十位个位,若百位十位个位有重复的,则不输出即可。 代…

关于计算机存储单位?

关于计算机存储单位? 计算机只能识别二进制。(1010100110. . . ) 1字节 8bit(8比特)–>1byte 8bit 1bit 就是一个 1 或 0 1KB 1024byte byte是[-128 ~ 127],共可以标识256个不同的数字。 byte类型的最大值是怎么计算出来的…

ffmpeg 命令转封装

1: 改变编码格式 原mp4文件:视频是h264 音频是aac 视频转成h265,音频转成mp3(容器为mkv,有些容器不一定支持放h265的) ffmpeg -i test_60s.mp4 -vcodec libx265 -acodec libmp3lame out_h265_mp3.mkv 播放&#xff1a…

Delphi 2010 DataSnap封装COM对象

在Delphi 2010中,DataSnap已完全可以不使用COM了.想起在windows上配置COM,就麻烦的很,如果在本机还好说,在远程要涉及到权限等诸多问题(用SocketConnection要方便一些). 如果早期写的程序中有许多COM对象,我们可以通过DataSnap的封装,使用适配器模式简单地封装一下,那么在客户端…

JavaScript中带有示例的Math.PI属性

JavaScript | Math.PI属性 (JavaScript | Math.PI Property) Math.PI is a property in math library of JavaScript that is used to find the value of PI(π) which is a mathematical constant whose value is 3.141. It is generally used to solve problems related to c…

设计模式笔记——Bridge

桥接模式Bridge Pattern 组合关系(实心菱形):强的拥有关系,体现了严格的整体和部分的关系,部分和整体的生命周期相同。 聚合关系(空心菱形):弱的拥有关系,A对象可以包含B…

实验7 视图操作

实验7 视图操作一、实验目的 1.了解视图的功能。 2.掌握创建和查看视图的方法。 3.掌握视图修改和删除视图的方法。 二、实验要求 创建student数据库中的相关视图。 三、实验步骤 1.在members表中创建地址为“湖南株洲”的会员的视图V_addr,SQL代码如下所示&#x…

从日志服务器接收的对 metaWeblog.newPost 方法的响应无效的解决方案

今天用windows Live Writer(WLW)写博客出现了“从日志服务器接收的对 metaWeblog.newPost 方法的响应无效”的故障。之前用的还好好的。于是我祭起google大法。从网上搜索了不少资料都是关于WP,没有关于z-blog。这些文章提到可能的问题是诸如插件冲突、utf编码之类的…

汇编语言-006(数组操作 、字符串应用、PUSHFD_POPFD 、PUSHAD_POPAD 、 子程序 函数、 USES 、 INC_DEC )

1: 计算斐波那契数列前7个数值之和 .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.data.code main PROCmov esi,1mov edi,1mov eax,2mov ecx,5 L1: mov ebx,esiadd ebx,edimov esi,edimov edi,ebxadd eax,ebxloop L1INVOKE ExitProcess,0 main END…

弗林的计算机体系结构分类

计算机体系结构分类 (Classification of computer architecture) According to Flynns there are four different classification of computer architecture, 根据弗林的说法,计算机体系结构有四种不同的分类, 1)SISD(单指令单数据流) (1) SISD (Single…