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();
        }
    }
}
相关推荐
暖馒5 小时前
Modbus应用层协议的深度剖析
网络·网络协议·c#·wpf·智能硬件
刘欣的博客8 小时前
C# CS架构程序发版升级的走数据库方案
c#·单文件升级自己的方式
Yorlen_Zhang9 小时前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
不绝1919 小时前
C#进阶:预处理指令/反射,Gettype,Typeof/关键类
开发语言·c#
大鹏说大话10 小时前
告别 MSBuild 脚本混乱:用 C# 和 Nuke 构建清晰、可维护的现代化构建系统
开发语言·c#
czhc114007566311 小时前
通信 28
c#
bugcome_com15 小时前
C# 程序结构详解:从 Hello World 开始
c#
唐梓航-求职中15 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#
bugcome_com18 小时前
阿里云 OSS C# SDK 使用实践与参数详解
阿里云·c#
懒人咖1 天前
缺料分析时携带用料清单的二开字段
c#·金蝶云星空