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();
        }
    }
}
相关推荐
hhb_61815 小时前
C#高性能异步编程实战与底层原理深度解析
开发语言·c#
beyond谚语15 小时前
反射、特性和依赖注入
c#
Tiger_shl16 小时前
C# 托管对象、非托管对象 讲解
开发语言·c#
LF男男16 小时前
Action- C# 内置的委托类型
java·开发语言·c#
2501_9307077820 小时前
使用C#代码在 PowerPoint 中创建组合图表
开发语言·c#·powerpoint
Full Stack Developme1 天前
Hutool DFA 教程
开发语言·c#
xiaoshuaishuai81 天前
【无标题】
开发语言·windows·c#
SunnyDays10111 天前
C# 如何快速比较 Word 文档并显示差异
c#·对比 word 文档·比较 word 文档
LF男男1 天前
TouchPad(单例)
unity·c#
武藤一雄1 天前
19个核心算法(C#版)
数据结构·windows·算法·c#·排序算法·.net·.netcore