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();
        }
    }
}
相关推荐
Charles_go4 小时前
41、C#什么是单例设计模式
java·设计模式·c#
夏霞4 小时前
c# ASP.NET Core SignalR 客户端与服务端自动重连配置指南
开发语言·c#·asp.net
Scout-leaf4 小时前
九成九新自用C#入门文档
c#
烛阴7 小时前
隐式vs显式:解密C#类型转换的底层逻辑
前端·c#
梦里不知身是客117 小时前
kafka作为Sink
c#·linq
猿来是你_L7 小时前
C# Dictionary 转换成 List
windows·c#·list
kokunka8 小时前
C#类修饰符功能与范围详解
java·开发语言·c#
mudtools8 小时前
.NET驾驭Excel之力:工作簿与工作表操作基础
c#·.net·excel
mudtools8 小时前
.NET驾驭Excel之力:单元格与区域操作详解
c#·.net·excel
用户83562907805111 小时前
C# 自动化生成 PowerPoint 演示文稿
后端·c#