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();
        }
    }
}
相关推荐
五花肉.15 分钟前
C#面试核心考点和回答要点
面试·c#
oioihoii1 小时前
从C++到C#的转型完全指南
开发语言·c++·c#
Traced back1 小时前
C#/.NET 常用控件、属性、方法和语句大全(或许全)
开发语言·c#·.net
jiayong233 小时前
Word图文混排实战技巧
开发语言·c#·word
阿蒙Amon4 小时前
C#每日面试题-Dictionary和Hashtable的区别
java·面试·c#
乐园游梦记4 小时前
工业视觉(尤其是 3D/2.5D 相机场景)中针对不同数据类型、精度、用途设计的保存格式
数码相机·opencv·3d·c#
爱说实话5 小时前
c# 20260113
开发语言·c#
阿蒙Amon5 小时前
C#每日面试题-简述命名空间和程序集
java·面试·c#
HEADKON5 小时前
玛伐凯泰mavacamten基于心脏功能监测的剂量调整可以降低心力衰竭风险
c#
DowneyJoy6 小时前
【Unity通用工具类】列表扩展方法ListExtensions
unity·c#·交互