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();
        }
    }
}
相关推荐
kaikaile19958 小时前
数字全息图处理系统(C# 实现)
开发语言·c#
wearegogog12313 小时前
C# .NET 文件比较工具 WinForms
开发语言·c#·.net
糖不吃13 小时前
WPF值转换器
c#
Popeye-lxw15 小时前
由罗技 K380 键盘 FN 键模式切换引发的血案
c#
FL162386312915 小时前
C# OpenCvSharp 基于霍夫变换直线检测的文本图像倾斜校正文本图像倾斜校
开发语言·c#
aini_lovee16 小时前
C# 快递单打印系统(万能套打系统)
开发语言·c#
白菜上路16 小时前
C# Serilog.AspNetCore基本使用
c#·serilog
小白不白11117 小时前
C# WinForm 与 VP 二次开发
开发语言·c#
SunnyDays101118 小时前
如何使用 C# 自动调整 Excel 行高和列宽
开发语言·c#·excel
itgather19 小时前
OfficeExcel — Word / Excel DLL 验证台功能介绍
c#·word·excel