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();
        }
    }
}
相关推荐
我是唐青枫8 分钟前
C#.NET ObjectPool 深入解析:对象复用、池化策略与使用边界
c#·.net
aq55356001 小时前
Laravel2.x:被遗忘的PHP框架遗珠
开发语言·汇编·c#
光泽雨1 小时前
c#对object sender ,EventArgs e 的解释
开发语言·c#
Eiceblue1 小时前
C# 删除 PDF 页面:单页 / 多页批量删除技巧
前端·pdf·c#
1314lay_10073 小时前
Vue+C#根据配置文件实现动态构建查询条件和动态表格
javascript·vue.js·elementui·c#
叫我黎大侠3 小时前
.NET 实战:调用千问视觉模型实现 OCR(车票识别完整教程)
阿里云·ai·c#·ocr·asp.net·.net·.netcore
唐青枫3 小时前
C#.NET ValueTaskSource 深入解析:零分配异步、ManualResetValueTaskSourceCore 与使用边界
c#·.net
公子小六3 小时前
基于.NET的Windows窗体编程之WinForms事件简介
windows·microsoft·c#·.net
军训猫猫头4 小时前
7.带输入参数的线程启动 C# + WPF 完整示例
开发语言·前端·c#·.net·wpf