C# lambda表达式 =>

属性

复制代码
using System;

namespace StructsSample
{
    public struct Dimensions
    {
        public double Length { get;  }
        public double Width { get; }

        public Dimensions(double length, double width)
        {
            Length = length;
            Width = width;
        }

        public double Diagonal => Math.Sqrt(Length * Length + Width * Width);
       //使用lambda表达式时,Diagonal是属性
    }
}

使用lambda表达式时,Diagonal是属性 ,可以用point.Diagonal的形式直接使用Diagonal的值

复制代码
namespace StructsSample
{
    class Program
    {
        static void Main()
        {
            var point = new Dimensions(3, 6);
           Console.WriteLine(point.Diagonal);//属性
          //  Console.WriteLine(point.Diagonal());//方法
            Console.ReadLine();
        }
    }
}

方法

复制代码
using System;

namespace StructsSample
{
    public struct Dimensions
    {
        public double Length { get;  }
        public double Width { get; }

        public Dimensions(double length, double width)
        {
            Length = length;
            Width = width;
        }

       // public double Diagonal => Math.Sqrt(Length * Length + Width * Width);
        public double Diagonal() //方法
        {
            double a;
            a= Math.Sqrt(Length * Length + Width * Width);
            return a;
        }
    }
}

public double Diagonal() 作为方法,调用point.Diagonal()值时要加上()

复制代码
namespace StructsSample
{
    class Program
    {
        static void Main()
        {
            var point = new Dimensions(3, 6);
            //Console.WriteLine(point.Diagonal);
            Console.WriteLine(point.Diagonal());
            Console.ReadLine();
        }
    }
}
相关推荐
影寂ldy11 小时前
C# 类和对象
开发语言·c#
z落落12 小时前
C# 构造函数(无参/有参/重载/this)+析构函数(终结器)|GC 垃圾回收
java·开发语言·c#
z落落13 小时前
C# 字段与属性(get/set访问器、三种属性写法、只读属性)+属性拦截例子(get动态计算 + set数据校验)
开发语言·c#
影寂ldy13 小时前
C#栈和队列
开发语言·c#
魔法阵维护师13 小时前
从零开发游戏需要学习的c#模块,第三十四章(设置界面)
学习·游戏·c#
gc_229913 小时前
学习C#调用OpenXml操作word文档的基本用法(39:学习表格类-1)
c#·word·表格·table·openxml
gc_229914 小时前
C#测试调用Net.Codecrete.QrCodeGenerator库生成二维码的基本用法
c#·二维码·qrcodegenerator
yivifu15 小时前
CSS 自动级联编号有序列表完全指南
前端·css·c#·html·有序列表·级联编号
Ws_16 小时前
C# 桌面端开发工程师面试题 + 参考答案
开发语言·面试·c#
周杰伦fans17 小时前
掌握 MVVM Light:.NET 桌面应用开发的 MVVM 利器,掌握 ObservableObject、RelayCommand 和 Messenger
c#·wpf