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();
        }
    }
}
相关推荐
yuanpan44 分钟前
同为.net/C#的跨平台运行时的mono和.net Core有什么区别?
c#·.net·.netcore
Hare_bai3 小时前
WPF的交互核心:命令系统(ICommand)
ui·c#·wpf·交互·xaml
Eiceblue4 小时前
C# 将HTML文档、HTML字符串转换为图片
visualstudio·c#·xhtml
进阶的小木桩18 小时前
C# 导出word 插入公式问题
开发语言·c#·word
天天代码码天天19 小时前
PP-OCRv5 C++封装DLL C#调用源码分享
开发语言·c++·c#·ocr
江沉晚呤时20 小时前
深入了解 C# 异步编程库 AsyncEx
java·前端·数据库·c#·.netcore
快乐飒男20 小时前
c#基础08(数组)
c#
Hare_bai20 小时前
WPF的UI交互基石:数据绑定基础
ui·c#·wpf·交互·xaml
归途醉染21 小时前
C# Costura.Fody 排除多个指定dll
开发语言·c#
CopyLower21 小时前
设计一个支持100,000 QPS的评论中台系统架构:技术实践与实现指南
系统架构·c#·linq