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();
        }
    }
}
相关推荐
will_net1 小时前
apollo 站点部署&C#接入
开发语言·c#·apollo
吾与谁归in8 小时前
C#脚本工具,支持智能提示自动补全(WPF + Roslyn + AvalonEdit)
开发语言·c#·wpf
昂好的day8 小时前
WPF 从零入门(上):WPF 是什么、XAML 语法、布局与样式全解
开发语言·c#·wpf
吃一碗粉只付一碗钱1 天前
10-令牌里的用户主键不要明文
服务器·数据库·sql·sqlserver·c#
胖纸不争1 天前
EnergyStarZ:让 Windows 后台进程给前台让路的能效管理器(v1.2 发布)
windows·性能优化·c#·win32·ef core
曹牧1 天前
C#:The operation has timed out
开发语言·c#
He BianGu1 天前
【WPF-Control教程】IOC 依赖注入
c#·wpf·wpf-control
He BianGu1 天前
【WPF-Control教程】H.Extensions.Behvaiors 开发文档
c#·wpf·wpf-control
He BianGu1 天前
【WPF-Control教程】H.Style 开发文档
c#·wpf·wpf-control