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();
        }
    }
}
相关推荐
魔法阵维护师3 小时前
从零开发游戏需要学习的c#模块,第二十章(2D 敌人与战斗触发)
学习·游戏·c#
我是唐青枫4 小时前
C#.NET YARP + OpenTelemetry:网关链路追踪实战
开发语言·c#·.net
Ws_5 小时前
C# 学习 Day1
开发语言·学习·c#
魔法阵维护师5 小时前
从零开发游戏需要学习的c#模块,第二十一章(精灵动画 —— 让角色走起来)
学习·游戏·c#
Eiceblue5 小时前
使用 C# 高效替换 PDF 中的文本:全页、区域与正则匹配
visualstudio·pdf·c#
一念春风7 小时前
.md文件浏览器
c#·wpf
jerryinwuhan7 小时前
SparkStream详细笔记
笔记·c#·linq
加号317 小时前
【C#】 串口通信技术深度解析及实现
开发语言·c#
无风听海18 小时前
C# 隐式转换深度解析
java·开发语言·c#
LateFrames19 小时前
520 - 如何说晚安 (WPF)
c#·wpf·浪漫·ui体验