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();
        }
    }
}
相关推荐
张鱼小丸子_微辣9 分钟前
Halcon/C# 图像窗口、读取图片及仿射变换
c#·halcon
CoderIsArt3 小时前
C# WPF常用调试工具汇总
开发语言·c#·wpf
gc_22995 小时前
C#测试调用ClosedXML根据批注设置excel单元格内容
c#·批注·closedxml
唐青枫7 小时前
C#.NET HttpClient 使用教程
c#·.net
夜空晚星灿烂8 小时前
C# 网络编程-关于HttpClient使用方式(三)
开发语言·网络·c#
甄天9 小时前
WPF Style样式 全局样式资源字典
c#·wpf
夜空晚星灿烂11 小时前
C# 网络编程-关于HttpWebRequest使用方式(二)
开发语言·c#
王中阳Go20 小时前
北京京东,看看难度
c#·linq
周杰伦fans1 天前
C# 中 string.Compare 比较两个字符串的字典顺序
c#
上位机付工1 天前
西门子S7通信协议抓包分析应用
c#·wireshark·上位机·plc·抓包·s7协议·西门子