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();
        }
    }
}
相关推荐
小张-森林人15 分钟前
C#Lambda表达式与委托关系
c#
小张-森林人1 小时前
C#中,什么是委托,什么是事件及它们之间的关系
开发语言·c#
code bean2 小时前
【C#】`Task.Factory.StartNew` 和 `Task.Run` 区别
前端·vue.js·c#
__XYZ3 小时前
Vala 编程语言教程-继承
c语言·开发语言·c++·c#
月巴月巴白勺合鸟月半4 小时前
工作记录 2017-03-10
c#·健康医疗
公子小六5 小时前
ASP.NET Core WebApi+React UI开发入门详解
react.js·ui·c#·asp.net·.netcore
工藤新一OL7 小时前
.netCore的winform程序如何调用webapi
c#·.net·.netcore·visual studio
绿叶白墙8 小时前
c# 动态库重名冲突处理方法 (起别名)
c#
hhcyyds118 小时前
【C#】变量和常量
c#
qq_3404740219 小时前
5.1 WPF路由事件以及文本样式
前端·c#·wpf