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 )
Classes are references types of data type, structures are value type of data type.
類是數據類型的引用類型,結構是數據類型的值類型。
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.
類支持默認構造函數,即我們可以設置將在創建對象時分配的默認值。 結構不支持默認構造函數的概念,我們無法在創建結構對象/變量時設置諸如類之類的可以用作默認值的值。
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