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();
        }
    }
}
相关推荐
He BianGu1 天前
【笔记】C# 中BindingFlags的详细作用
笔记·c#
?abc!1 天前
(贪心)621. 任务调度器
java·前端·c#
开开心心就好1 天前
电脑小说阅读器,支持朗读和自定义皮肤
服务器·开发语言·javascript·算法·pdf·c#·音视频
淡海水1 天前
07-06-源码-.NET Core GC的极致优化技术
unity·架构·c#·.netcore·gc·1024程序员节
万化冥合1 天前
怎么批量把 Word 转 PDF?
python·pdf·c#·word
淡海水2 天前
07-02-源码-.NET Core GC分配路径
算法·unity·c#·.netcore·gc·1024程序员节
吴可可1232 天前
C#调用摄像头拍照的三种API方案
c#
Msir_Modern2 天前
Noita 元胞沙盒笔记
算法·unity·c#·游戏引擎·元胞自动机
星之擎2 天前
Task 很优雅的取消方式是什么?C# 里面。
开发语言·c#
czhc11400756632 天前
一周概念复盘:预热、漂移与门禁
c#