首页 > 其他 > 详细

结构函数

时间:2020-01-01 22:20:06      阅读:69      评论:0      收藏:0      [点我收藏+]

结构类型不但可以用来存储数据元素,还可以用来包含函数。

举一个例子,

struct CustomerName
{
        public string firstName,lastName;
}

static void Main(string [] args)
{
        CustomerName myCustomer;
    
        myCustomer.firstName = "John";
        myCustomer.lastName = "Franklin"
        WriteLine($"{myCustomer.firstName} {myCustomer.lastName}");

}

这里显然有些繁琐。如果我们使用结构包含函数,语法就会简单很多。

struct CustomerName
{
        public string firstName,lastName;
        public string Name() =>firstName + " "+lastName;
}

static void Main(string [] args)
{
        CustomerName myCustomer;
    
        myCustomer.firstName = "John";
        myCustomer.lastName = "Franklin"
        WriteLine(myCustomer.Name());

}    

结构函数

原文:https://www.cnblogs.com/Mr-Prince/p/12130279.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!