最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# 如何使用 Index 和 Range 簡化集合操作

 更新時間:2021年02月25日 08:27:10   作者:WeihanLi  
這篇文章主要介紹了C# 如何使用 Index 和 Range 簡化集合操作,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

Intro

有的語言數(shù)組的索引值是支持負(fù)數(shù)的,表示從后向前索引,比如:arr[-1]

從 C# 8 開始,C# 支持了數(shù)組的反向 Index,和 Range 操作,反向 Index 類似于其他語言中的負(fù)索引值,但其實是由編譯器幫我們做了一個轉(zhuǎn)換,Range 使得我們對數(shù)組截取某一部分的操作會非常簡單,下面來看一下如何使用吧

Sample

使用 ^ 可以從集合的最后開始索引元素,如果從數(shù)組的最后開始索引元素,最后一個元素應(yīng)該是 1 而不是0如: arr[^1]

使用 .. 可以基于某個數(shù)組截取集合中的某一段創(chuàng)建一個新的數(shù)組,比如 var newArray = array[1..^1],再來看一下下面的示例吧

int[] someArray = new int[5] { 1, 2, 3, 4, 5 };
int lastElement = someArray[^1]; // lastElement = 5
lastElement.Dump();

someArray[3..5].Dump();

someArray[1..^1].Dump();

someArray[1..].Dump();

someArray[..^1].Dump();

someArray[..2].Dump();

輸出結(jié)果如下:

Index

那么它是如何實現(xiàn)的呢,索引值引入了一個新的數(shù)據(jù)結(jié)構(gòu) System.Index,當(dāng)你使用 ^ 運算符的時候,實際轉(zhuǎn)換成了 Index。

Index:

public readonly struct Index : IEquatable<Index>
{
 public Index(int value, bool fromEnd = false);

 /// <summary>Create an Index pointing at first element.</summary>
 public static Index Start => new Index(0);

 /// <summary>Create an Index pointing at beyond last element.</summary>
 public static Index End => new Index(~0);
 //
 // Summary:
 //  Gets a value that indicates whether the index is from the start or the end.
 //
 // Returns:
 //  true if the Index is from the end; otherwise, false.
 public bool IsFromEnd { get; }
 //
 // Summary:
 //  Gets the index value.
 //
 // Returns:
 //  The index value.
 public int Value { get; }

 //
 // Summary:
 //  Creates an System.Index from the end of a collection at a specified index position.
 //
 // Parameters:
 // value:
 //  The index value from the end of a collection.
 //
 // Returns:
 //  The Index value.
 public static Index FromEnd(int value);
 //
 // Summary:
 //  Create an System.Index from the specified index at the start of a collection.
 //
 // Parameters:
 // value:
 //  The index position from the start of a collection.
 //
 // Returns:
 //  The Index value.
 public static Index FromStart(int value);
 //
 // Summary:
 //  Returns a value that indicates whether the current object is equal to another
 //  System.Index object.
 //
 // Parameters:
 // other:
 //  The object to compare with this instance.
 //
 // Returns:
 //  true if the current Index object is equal to other; false otherwise.
 public bool Equals(Index other);
 //
 // Summary:
 //  Calculates the offset from the start of the collection using the given collection length.
 //
 // Parameters:
 // length:
 //  The length of the collection that the Index will be used with. Must be a positive value.
 //
 // Returns:
 //  The offset.
 public int GetOffset(int length);

 //
 // Summary:
 //  Converts integer number to an Index.
 //
 // Parameters:
 // value:
 //  The integer to convert.
 //
 // Returns:
 //  An Index representing the integer.
 public static implicit operator Index(int value);
}

如果想要自己自定義的集合支持 Index 這種從數(shù)組最后索引的特性,只需要加一個類型是 Index 的索引器就可以了,正向索引也是支持的,int 會自動隱式轉(zhuǎn)換為 Index,除了顯示的增加 Index 索引器之外,還可以隱式支持,實現(xiàn)一個 int Count {get;} 的屬性(屬性名叫 Length 也可以),在實現(xiàn)一個 int 類型的索引器就可以了

寫一個簡單的小示例:

private class TestCollection
{
 public IList<int> Data { get; init; }

 public int Count => Data.Count;
 public int this[int index] => Data[index];

 //public int this[Index index] => Data[index.GetOffset(Data.Count)];
}
var array = new TestCollection()
{
 Data = new[] { 1, 2, 3 }
};
Console.WriteLine(array[^1]);
Console.WriteLine(array[1]);

Range

Range 是在 Index 的基礎(chǔ)上實現(xiàn)的,Range 需要兩個 Index 來指定開始和結(jié)束

public readonly struct Range : IEquatable<Range>
{
 /// <summary>Represent the inclusive start index of the Range.</summary>
 public Index Start { get; }

 /// <summary>Represent the exclusive end index of the Range.</summary>
 public Index End { get; }

 /// <summary>Construct a Range object using the start and end indexes.</summary>
 /// <param name="start">Represent the inclusive start index of the range.</param>
 /// <param name="end">Represent the exclusive end index of the range.</param>
 public Range(Index start, Index end)
 {
  Start = start;
  End = end;
 }

 /// <summary>Create a Range object starting from start index to the end of the collection.</summary>
 public static Range StartAt(Index start) => new Range(start, Index.End);

 /// <summary>Create a Range object starting from first element in the collection to the end Index.</summary>
 public static Range EndAt(Index end) => new Range(Index.Start, end);

 /// <summary>Create a Range object starting from first element to the end.</summary>
 public static Range All => new Range(Index.Start, Index.End);

 /// <summary>Calculate the start offset and length of range object using a collection length.</summary>
 /// <param name="length">The length of the collection that the range will be used with. length has to be a positive value.</param>
 /// <remarks>
 /// For performance reason, we don't validate the input length parameter against negative values.
 /// It is expected Range will be used with collections which always have non negative length/count.
 /// We validate the range is inside the length scope though.
 /// </remarks>
 public (int Offset, int Length) GetOffsetAndLength(int length);
}

如何在自己的類中支持 Range 呢?

一種方式是自己直接實現(xiàn)一個類型是 Range 的索引器

另外一種方式是隱式實現(xiàn),在自定義類中添加一個 Count 屬性,然后實現(xiàn)一個 Slice 方法,Slice 方法有兩個 int 類型的參數(shù),第一個參數(shù)表示 offset,第二個參數(shù)表示 length

來看下面這個示例吧,還是剛才那個類,我們支持一下 Range:

private class TestCollection
{
 public IList<int> Data { get; init; }
 //public int[] this[Range range]
 //{
 // get
 // {
 //  var rangeInfo = range.GetOffsetAndLength(Data.Count);
 //  return Data.Skip(rangeInfo.Offset).Take(rangeInfo.Length).ToArray();
 // }
 //}

 public int Count => Data.Count;

 public int[] Slice(int start, int length)
 {
  var array = new int[length];
  for (var i = start; i < length && i < Data.Count; i++)
  {
   array[i] = Data[i];
  }
  return array;
 }
}

More

新的操作符 (^ and ..) 都只是語法糖,本質(zhì)上是調(diào)用 Index、Range

Index 并不是支持負(fù)數(shù)索引,從最后向前索引只是編譯器幫我們做了一個轉(zhuǎn)換,轉(zhuǎn)換成從前到后的索引值,借助于它,我們很多取集合最后一個元素的寫法就可以大大的簡化了,就可以從原來的 array[array.Length-1] => array[^1]

Range 在創(chuàng)建某個數(shù)組的子序列的時候就非常的方便, newArray = array[1..3],需要注意的是,Range 是"左閉右開"的,包含左邊界的值,不包含右邊界的值

還沒使用過 Index/Range 的快去體驗一下吧,用它們優(yōu)化數(shù)組的操作吧~~

以上就是C# 如何使用 Index 和 Range 簡化集合操作的詳細(xì)內(nèi)容,更多關(guān)于c# 使用 Index 和 Range 簡化集合操作的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:

相關(guān)文章

  • C#實現(xiàn)格式化SQL語句的示例代碼

    C#實現(xiàn)格式化SQL語句的示例代碼

    這篇文章主要為大家詳細(xì)介紹了C#如何實現(xiàn)格式化SQL語句的功能,文中的示例代碼簡潔易懂,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-08-08
  • C#委托delegate實例解析

    C#委托delegate實例解析

    這篇文章主要介紹了C#委托delegate實例解析,對C#的委托做了較為深入的剖析,很適合初學(xué)者學(xué)習(xí),需要的朋友可以參考下
    2014-08-08
  • C#正則表達(dá)式實用大全(建議收藏!)

    C#正則表達(dá)式實用大全(建議收藏!)

    正則表達(dá)式是處理字符串的強大工具,擁有獨特的語法和獨立的處理引擎,下面這篇文章主要給大家介紹了關(guān)于C#正則表達(dá)式實用大全的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • Task提高異步執(zhí)行效率技巧

    Task提高異步執(zhí)行效率技巧

    這篇文章介紹了Task提高異步執(zhí)行效率的技巧,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C# winform編程中響應(yīng)回車鍵的實現(xiàn)代碼

    C# winform編程中響應(yīng)回車鍵的實現(xiàn)代碼

    這篇文章主要介紹了C# winform編程中響應(yīng)回車鍵的實現(xiàn)代碼,既在窗口上響應(yīng)回車鍵事件的方法,需要的朋友可以參考下
    2014-08-08
  • C#類的訪問修飾符用法分析

    C#類的訪問修飾符用法分析

    這篇文章主要介紹了C#類的訪問修飾符用法,較為詳細(xì)的分析了C#類的訪問修飾符概念與用法,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-10-10
  • C#實現(xiàn)目錄跳轉(zhuǎn)(TreeView和SplitContainer)的示例代碼

    C#實現(xiàn)目錄跳轉(zhuǎn)(TreeView和SplitContainer)的示例代碼

    本文主要介紹了C#實現(xiàn)目錄跳轉(zhuǎn)(TreeView和SplitContainer)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • C#中兩個byte如何相加

    C#中兩個byte如何相加

    可能有的看到這個題目就會覺得這不簡單嗎?直接用+號相加就行了,可是當(dāng)你實際操作運行的時候就會發(fā)現(xiàn)有錯誤了,那么是什么錯誤?那該如何讓C#中兩個byte相加呢?通過下面這篇文章來一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-11-11
  • .net的序列化與反序列化實例

    .net的序列化與反序列化實例

    這篇文章主要介紹了.net的序列化與反序列化實例,詳細(xì)講述了.net序列化與反序列化的基本原理與用法實例,具有很好的參考借鑒價值,需要的朋友可以參考下
    2014-10-10
  • C#動態(tài)查詢之巧用Expression組合多條件表達(dá)式的方法和步驟

    C#動態(tài)查詢之巧用Expression組合多條件表達(dá)式的方法和步驟

    在C#中,可以使用AndAlso和OrElse方法組合兩個Expression<Func<T, bool>>類型的表達(dá)式,下面通過實例代碼給大家分享C#動態(tài)查詢之巧用Expression組合多條件表達(dá)式,感興趣的朋友跟隨小編一起看看吧
    2024-05-05

最新評論

杭锦后旗| 谢通门县| 禹州市| 栾川县| 海林市| 凤冈县| 竹溪县| 娱乐| 余江县| 合江县| 浙江省| 荣昌县| 涿州市| 综艺| 天等县| 兴安县| 清镇市| 马关县| 秀山| 新营市| 化隆| 保山市| 岗巴县| 辉南县| 通化市| 阳春市| 周宁县| 基隆市| 田林县| 聂荣县| 偏关县| 陇川县| 庄浪县| 界首市| 成安县| 亳州市| 东台市| 渝中区| 塔河县| 上饶县| 鱼台县|