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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值