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();
        }
    }
}
相关推荐
ytttr87312 小时前
C# 定时数据库备份工具
开发语言·数据库·c#
雪豹阿伟14 小时前
21.Winfrom —— 定时器、日期选择器、进度条、表格、DataTable
c#·上位机·winfrom
z落落14 小时前
C#WinForm控件实战:Panel与单选框动态创建
开发语言·c#
qq_4221525716 小时前
Word 文件太大怎么压缩?2026 年文档瘦身方案对比
开发语言·c#·word
影寂ldy18 小时前
C# 事件完整学习笔记(发布订阅 + 自定义事件 + 内置 EventHandler)
笔记·学习·c#
kyle~19 小时前
DDS分布式实时系统---自省机制
开发语言·分布式·机器人·c#·接口·ros2
Java面试题总结20 小时前
MarkItDown 再次登顶GitHub榜
开发语言·c#·github
xiaoshuaishuai820 小时前
C# 定制化Markdown编辑器
开发语言·c#·编辑器
yugi98783821 小时前
基于C#实现数字识别率的OCR方案
开发语言·c#·ocr
天天代码码天天1 天前
OpenCV 5 + PP-OCRv6 + OpenVINO:C# 本地 OCR 推理更快、更稳、更好集成
opencv·c#·openvino