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();
        }
    }
}
相关推荐
曹牧1 小时前
C#:无法从方法组转换为objec
开发语言·c#
刘欣的博客4 小时前
C# 从API接口获取对象而不用先定义对象类
c#·json动态创建对象
Charles_go4 小时前
C#中级、double和decimal有什么区别
开发语言·c#
小熊熊知识库12 小时前
C#接入AI操作步骤详解(deepseek接入)
人工智能·flask·c#
玖笙&12 小时前
✨WPF编程进阶【7.3】集成动画(附源码)
c++·c#·wpf·visual studio
yue00814 小时前
C# 窗体渐变色
开发语言·javascript·c#
czhc114007566314 小时前
C#1119记录 类 string.Split type.TryParse(String,out type 变量)
android·c#
WangMing_X16 小时前
C#一个项目实现文件目录常用操作(附源码完整)
开发语言·c#
mudtools17 小时前
.NET集成飞书API最佳实践:基于Mud.Feishu的飞书二次开发实践
c#·.net
玩泥巴的17 小时前
.NET项目中如何快速的集成飞书API
c#·.net·飞书