属性
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();
}
}
}