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.pswp.cn/news/379419.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379419.shtml
英文地址,請注明出處:http://en.pswp.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…