日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

表达式主体定义

發(fā)布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 表达式主体定义 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原文地址https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members

通過表達式主體定義,可采用非常簡潔的可讀形式提供成員的實現(xiàn)。?只要任何支持的成員(如方法或屬性)的邏輯包含單個表達式,就可以使用表達式主體定義。?表達式主體定義具有下列常規(guī)語法:

C#

member => expression;

其中“expression”是有效的表達式。

C# 6 中引入了針對方法和屬性 Get 訪問器的表達式主體定義支持,并在 C# 7.0 中進行了擴展。?表達式主體定義可用于下表列出的類型成員:

成員開始提供支持的版本
方法C# 6
構造函數(shù)C# 7.0
終結器C# 7.0
屬性 GetC# 6
屬性 SetC# 7.0
索引器C# 7.0

方法

expression-bodied 方法包含單個表達式,它返回的值的類型與方法的返回類型匹配;或者,對于返回?void?的方法,其表達式則執(zhí)行某些操作。?例如,替代?ToString?方法的類型通常包含單個表達式,該表達式返回當前對象的字符串表示形式。

下面的示例定義?Person?類,該類通過表達式主體定義替代?ToString。?它還定義向控制臺顯示名稱的?DisplayName?方法。?請注意,ToString?表達式主體定義中未使用?return?關鍵字。

C#

  • using System;

  • ?
  • public class Person

  • {

  • public Person(string firstName, string lastName)

  • {

  • fname = firstName;

  • lname = lastName;

  • }

  • ?
  • private string fname;

  • private string lname;

  • ?
  • public override string ToString() => $"{fname} {lname}".Trim();

  • public void DisplayName() => Console.WriteLine(ToString());

  • }

  • ?
  • class Example

  • {

  • static void Main()

  • {

  • Person p = new Person("Mandy", "Dejesus");

  • Console.WriteLine(p);

  • p.DisplayName();

  • }

  • }

  • ?
  • 有關詳細信息,請參閱方法(C# 編程指南)。

    構造函數(shù)

    構造函數(shù)的表達式主體定義通常包含單個賦值表達式或一個方法調(diào)用,該方法調(diào)用可處理構造函數(shù)的參數(shù),也可初始化實例狀態(tài)。

    以下示例定義?Location?類,其構造函數(shù)具有一個名為“name”的字符串參數(shù)。?表達式主體定義向?Name?屬性分配參數(shù)。

    C#

  • public class Location

  • {

  • private string locationName;

  • ?
  • public Location(string name) => Name = name;

  • ?
  • public string Name

  • {

  • get => locationName;

  • set => locationName = value;

  • }

  • }

  • 有關詳細信息,請參閱構造函數(shù)(C# 編程指南)。

    終結器

    終結器的表達式主體定義通常包含清理語句,例如釋放非托管資源的語句。

    下面的示例定義了一個終結器,該終結器使用表達式主體定義來指示已調(diào)用該終結器。

    C#

  • using System;

  • ?
  • public class Destroyer

  • {

  • public override string ToString() => GetType().Name;

  • ?
  • ~Destroyer() => Console.WriteLine($"The {ToString()} destructor is executing.");

  • }

  • 有關詳細信息,請參閱終結器(C# 編程指南)。

    屬性 Get 語句

    如果選擇自行實現(xiàn)屬性 Get 訪問器,可以對只返回屬性值的單個表達式使用表達式主體定義。?請注意,未使用?return?語句。

    下面的示例定義?Location.Name?屬性,其屬性 Get 訪問器返回支持該屬性的私有?locationName?字段的值。

    C#

  • public class Location

  • {

  • private string locationName;

  • ?
  • public Location(string name) => Name = name;

  • ?
  • public string Name

  • {

  • get => locationName;

  • set => locationName = value;

  • }

  • }

  • 不使用顯式?set?語句也可實現(xiàn)使用表達式主體定義的只讀屬性。?語法為:

    C#復制

    PropertyName => returnValue;

    下面的示例定義?Location?類,其只讀?Name?屬性以表達式主體定義的形式實現(xiàn),該表達式主體定義返回私有?locationName?字段值。

    C#

  • public class Location

  • {

  • private string locationName;

  • ?
  • public Location(string name) => locationName = name;

  • ?
  • public string Name => locationName;

  • }

  • 有關詳細信息,請參閱屬性(C# 編程指南)。

    屬性 Set 語句

    如果選擇自行實現(xiàn)屬性 Set 訪問器,可以對單行表達式使用表達式主體定義,該單行表達式用于對支持該屬性的字段賦值。

    下面的示例定義?Location.Name?屬性,其屬性 Set 語句將其輸入?yún)?shù)賦給支持該屬性的私有?locationName?字段。

    C#

  • public class Location

  • {

  • private string locationName;

  • ?
  • public Location(string name) => Name = name;

  • ?
  • public string Name

  • {

  • get => locationName;

  • set => locationName = value;

  • }

  • }

  • 有關詳細信息,請參閱屬性(C# 編程指南)。

    索引器

    與屬性一樣,如果索引器的 Get 訪問器包含單個返回值的語句或其 Set 訪問器執(zhí)行簡單的賦值,則 Get 和 Set 訪問器包含表達式主體定義。

    下面的示例定義名為?Sports?的類,其中包含一個內(nèi)部?String?數(shù)組,該數(shù)組包含大量體育運動的名稱。索引器的 Get 和 Set 訪問器都以表達式主體定義的形式實現(xiàn)。

    C#

  • using System;

  • using System.Collections.Generic;

  • ?
  • public class Sports

  • {

  • private string[] types = { "Baseball", "Basketball", "Football",

  • "Hockey", "Soccer", "Tennis",

  • "Volleyball" };

  • ?
  • public string this[int i]

  • {

  • get => types[i];

  • set => types[i] = value;

  • }

  • ?
  • 總結

    以上是生活随笔為你收集整理的表达式主体定义的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。