元編程參考文件

資料型別

穩定

列表、字典、集合,以及值在不同型別之間的轉換

Methodox Wiki 上的使用指南

10 個類別,共 222 個節點

所屬發行版
元編程(教育版)元編程(專業版)
支援等級
第 1 級(擬定)
所屬包
標準庫
標準庫

數字、文字、日期、陣列、列表、字典、集合與型別轉換——幾乎每張圖都需要它們。

可列舉序列操作

全部滿足Whether every element passes a test; it stops at the first that does not.存在任一項Whether the sequence has any elements at all; it reads at most one, so it is the cheap way to test for emptiness.追加The sequence with one more element at the end; the source itself is not changed.作為序列Passes the sequence through unchanged but typed as a plain sequence, hiding its concrete collection type so that later steps treat it only as something to enumerate.平均值The mean of the numbers, as a double so that it keeps its fraction.強制轉換Treats every element of an untyped sequence as a T Result, for when all elements are known to be of that type.分塊Splits the sequence into consecutive batches of Size elements, for example to process items a page at a time; the last batch is shorter when the length is not a multiple of the size.連線序列Joins two sequences end to end, keeping duplicates; use EnumerableRoutines.Union to merge without duplicates.包含Whether the sequence holds an element equal to Value, by the elements' default equality.計數How many elements the sequence has.為空時取預設值The sequence unchanged when it has any elements; when it is empty, a sequence holding exactly one default value instead (null for reference and nullable types, zero or false for numbers and booleans), so that a later step always has something to work on.去重The sequence with repeated elements removed, comparing elements by their default equality; the first occurrence of each is kept, in the order they first appear.按鍵去重One element per key, for example one person per surname: the first element seen with each key is kept, in their original order.指定位置的元素The element at a position, counting from zero.指定位置的元素或預設值The element at a position, counting from zero, or the type's default value (null for reference and nullable types, zero for numbers) when Index is negative or past the end.空序列A sequence with no elements, for use as a starting point or as a placeholder where a sequence input is required.差集The distinct elements of First that do not appear in Second, in the order of First.按鍵求差集The elements of First whose key is not in a list of keys, for example everyone except the IDs in an exclusion list; one element per key, the first seen, in the order of First.第一項The first element.第一項或預設值The first element, or the type's default value (null for reference and nullable types, zero for numbers) when the sequence is empty.分組Gathers elements that share a key into groups, for example orders by customer.分組連線Pairs each element of Outer with all the elements of Inner that share its key, for example each customer with their orders.交集The distinct elements of First that also appear in Second, in the order of First.按鍵求交集The elements of First whose key appears in a list of keys, for example the people whose IDs are in a selection; one element per key, the first seen, in the order of First.連線Matches elements of two sequences by key (an inner join), for example orders to their customers: every pair whose keys are equal gives one result.最後一項The last element.最後一項或預設值The last element, or the type's default value (null for reference and nullable types, zero for numbers) when the sequence is empty.大整數計數How many elements the sequence has, as a 64-bit number for sequences too long for EnumerableRoutines.Count.最大值The largest element, by the elements' default comparison.最小值The smallest element, by the elements' default comparison.篩選型別Keeps only the elements of an untyped sequence that are a T Result (or derive from it); nulls and elements of any other type are dropped, so unlike EnumerableRoutines.Cast it never throws for a mismatched element.排序The elements sorted from smallest to largest by their default comparison (numbers by value, text in the current culture's order).按鍵排序Sorts elements from smallest to largest by a key worked out from each, for example people by age.按鍵降序排序Sorts elements from largest to smallest by a key worked out from each, for example scores from highest to lowest.降序排序The elements sorted from largest to smallest by their default comparison.前置The sequence with one more element at the start; the source itself is not changed.整數範圍A run of consecutive whole numbers: Count of them, starting at Start, so a start of 1 and a count of 10 gives 1 to 10.重複A sequence holding the same value Count times.反轉The elements in reverse order.投影Transforms each element through Selector, for example from a list of people to a list of their names.投影並展平Turns each element into a sequence and joins all those sequences end to end into one flat sequence, for example from a list of folders to one list of all their files.唯一項The one and only element, for when having exactly one is an expectation worth checking.唯一項或預設值The only element, or the type's default value (null for reference and nullable types, zero for numbers) when the sequence is empty.跳過The sequence without its first Count elements.跳過末尾The sequence without its last Count elements.條件跳過Leaves out elements from the start for as long as Predicate answers true, then gives the first element it rejected and every element after it, whether or not they would pass.求和The total of the numbers; an empty sequence gives 0.取前若干項The first Count elements of the sequence: fewer when it is shorter, and none when the count is zero or less.取末尾若干項The last Count elements of the sequence: fewer when it is shorter, and none when the count is zero or less.條件取得Elements from the start for as long as Predicate answers true; it stops at the first element that fails, even if later elements would pass.再按鍵排序Refines an existing sort: elements that tie on the earlier keys are put in ascending order of this further key, for example surname then first name.再按鍵降序排序Refines an existing sort: elements that tie on the earlier keys are put in descending order of this further key.轉為陣列Reads the whole sequence now and copies its elements into a new array, so later steps work on a fixed snapshot instead of re-running the query.轉為字典Reads the whole sequence into a new dictionary, keying each element by Key Selector.轉為集合Reads the whole sequence into a new set, dropping duplicates, for fast membership tests or further set operations.轉為列表Reads the whole sequence now and copies its elements into a new list, so later steps work on a fixed snapshot that can also be added to.轉為查詢表Reads the whole sequence now into a lookup: a fixed, read-only grouping in which each key can have many elements, for example all orders per customer.並集Every distinct element found in either sequence: those of First in order, then those of Second not already seen.按鍵求並集Merges two sequences keeping one element per key, for example one entry per ID across two lists; the first element seen with each key is kept, those of First before those of Second.篩選Filters a sequence: only the elements for which Predicate answers true, in their original order.

列表操作

新增Appends an element to the end of the list.新增多項Appends every element of a sequence to the end of the list, keeping their order.作為只讀列表A read-only view of the list, for handing its contents on without letting the receiver change them.二分查詢Finds a value in a list that is already sorted in ascending order, much faster than a front-to-back search on a long list.容量How many elements the list can hold before it has to grow its storage; always at least its count.清空Removes every element, leaving an empty list; its reserved capacity is kept.複製列表A new list with the same elements in the same order, so the copy can be changed without affecting the original.包含Whether the list holds an element equal to the given value.全部轉換A new list made by passing each element through a converter, for turning a list of one type into another, such as numbers into labels.複製到陣列Copies every element of the list into an existing array, starting at the array's first slot.項數How many elements the list currently holds.建立列表A new, empty list that you can add elements to.確保容量Makes sure the list has room for at least the given number of elements, growing its storage if it is smaller; the elements themselves are not changed.存在滿足條件的項Whether at least one element passes a test, for questions such as "is any value negative?".查詢The first element, from the front, that passes a test.查詢索引The position of the first element that passes a test, searching from the front.查詢最後一個The last element, searching from the back, that passes a test.第一項The first element of the list.逐項執行Runs an action once for each element, front to back, for side effects such as printing or accumulating.取得項The element at a position in the list, counting from 0.取得範圍A new list holding a run of consecutive elements copied from this one.項的索引The position of the first element equal to the given value, searching from the front.插入Inserts an element at a position, moving the element there and everything after it one place along.插入多項Inserts every element of a sequence at a position, keeping their order and moving the existing elements from there onwards along to make room.為空Whether the list has no elements.最後一項The last element of the list.項的最後索引The position of the last element equal to the given value, searching from the back.移除Removes the first element equal to the given value, if there is one; later duplicates stay.移除所有滿足條件的項Removes every element that passes a test, for filtering a list in place.移除指定位置的項Removes the element at a position, moving everything after it one place back.移除範圍Removes a run of consecutive elements, moving everything after them back to close the gap.反轉Reverses the order of all elements in place, so the last becomes first.設定項Replaces the element at a position in the list with a new value.設定容量Sets exactly how many elements the list has room for, to reserve space ahead of time or release unused space.切片A new list holding a run of consecutive elements copied from this one; the same as Get Range, named for those used to slicing.排序Sorts the list in place in ascending order: numbers smallest first, text alphabetically.轉為陣列The list's elements copied into a new array, in the same order, for nodes that take an array.釋放多餘容量Shrinks the list's reserved storage to fit its elements, to give memory back once a list has finished growing.全部滿足條件Whether every element passes a test, for questions such as "are all values positive?".嘗試取得第一項Reads the first element if the list has one, without throwing on an empty list.嘗試取得項Reads the element at a position if there is one, without throwing when the position is out of range.嘗試取得最後一項Reads the last element if the list has one, without throwing on an empty list.

型別轉換

真否轉文字Converts a boolean to its string representation.位元組轉文字Converts a byte to its string representation.字元轉文字Converts a char to its string representation.轉換值型別Converts the specified input to the type T.高精度數字轉文字Converts a decimal to its string representation.數字轉文字按所有機器通用的唯一寫法把數字寫成文字:小數點寫作點號,不加千位分隔,例如 1.5。低精度數字轉文字Converts a float to its string representation.整數轉文字Converts an integer to its string representation.大整數轉文字Converts a long to its string representation.小整數轉文字Converts a short to its string representation.有符號位元組轉文字Converts an sbyte to its string representation.文字轉真否Converts a string to a boolean.文字轉位元組Converts a string to a byte-sized integer.文字轉字元Converts a string containing a single character to a char.文字轉高精度數字Converts a string to a decimal.文字轉數字按所有機器通用的唯一寫法從文字中讀取數字:小數點寫作點號,不加千位分隔,例如 1.5 或 -2.25e3。文字轉低精度數字Converts a string to a float.文字轉整數Converts a string to an integer.文字拆分為行文字轉大整數Converts a string to a long.文字轉小整數Converts a string to a short.文字轉有符號位元組Converts a string to a signed byte (sbyte).文字轉正整數Converts a string to an unsigned integer (uint).文字轉大正整數Converts a string to an unsigned long (ulong).文字轉小正整數Converts a string to an unsigned short (ushort).值轉位元組值轉高精度數字值轉數字值轉低精度數字值轉整數值轉大整數值轉小整數值轉有符號位元組值轉正整數值轉大正整數值轉小正整數正整數轉文字Converts a uint to its string representation.大正整數轉文字Converts an ulong to its string representation.小正整數轉文字Converts a ushort to its string representation.位元組轉 UTF-8 文字Converts UTF8 bytes to its string representation.UTF-8 文字轉位元組Converts a UTF8 encoded string to bytes.

字典例程

新增Adds a new entry, throwing System.ArgumentException if the dictionary already has the key, so an accidental duplicate is caught rather than silently overwriting.新增,重複則拒絕Adds a new entry like DictionaryRoutines.TryAdd, and when the key already exists also gives you a message naming the duplicate key, ready to show or log; the existing entry is left untouched.新增多項Adds many key/value pairs at once, in the order given.容量How many entries the dictionary can hold before it next has to grow its storage; always at least its count.清空Removes every entry; the dictionary keeps its current capacity, so refilling it does not have to grow it again.複製字典Makes a new dictionary holding the same entries as Source; adding or removing entries in one afterwards does not affect the other, but the values themselves are not copied, so a value of a reference type is shared by both.比較器The comparer the dictionary uses to decide whether two keys are the same, so you can make another dictionary that matches keys the same way.包含鍵Whether the dictionary has an entry for Key, matched with the dictionary's key comparer.包含值Whether any entry has Value as its value, compared with the value type's default equality.項數How many key/value pairs the dictionary holds.建立字典Makes a new, empty dictionary whose keys are compared with the key type's default equality.確保容量Grows the dictionary's storage, if needed, so it can hold at least Capacity entries without growing again; it never shrinks it.取得值The value stored under Key; use it when the key must be there, and DictionaryRoutines.GetOrDefault or DictionaryRoutines.TryGet when it may not be.取得值或預設值The value stored under Key, or Default Value when the dictionary has no such key.所有鍵The dictionary's keys, in no guaranteed order.移除Removes the entry for Key, if there is one.設定值Stores Value under Key, adding a new entry or replacing the value already there; use DictionaryRoutines.Add instead when a duplicate key should be an error.轉為陣列Copies every key/value pair into a new array, a snapshot that later changes to the dictionary do not affect.釋放多餘容量Shrinks the dictionary's storage to fit the entries it holds now, giving back memory after many removals.嘗試新增Adds a new entry only if the key is not already there; an existing entry is left untouched.嘗試取得值Looks up Key without failing when it is missing, telling you both whether it was found and what it holds.所有值The dictionary's values, in the same order as DictionaryRoutines.Keys.

元組操作

比較Orders two tuples of the same type element by element: the first position where they differ decides, as in sorting by several columns.比較物件Orders two values when you only have them as plain objects, using the left value's own comparison; null sorts before everything else.建立元組Makes the empty tuple, which holds no elements; it is the tuple form of "nothing", and every empty tuple equals every other.相等Whether two tuples of the same type hold equal elements, position by position, using each element type's default equality.格式化Writes a tuple as text, its elements separated by commas inside parentheses, for example (1, apple, True).取得項The element at a position in a tuple, counting from zero; use ValueTupleRoutines.TryGet when the position may be out of range.雜湊A hash code combining the tuple's elements, so equal tuples get equal codes; use it for bucketing or quick inequality checks.是否為元組Whether Value is a tuple, so the other tuple routines can read it.是否為元組型別Whether Type is a value tuple type of any size, from the empty tuple up to seven elements plus a nested rest; for checking a type before you have a value of it.長度How many elements a tuple holds, counting through the nested rest of a tuple with more than seven elements.結構比較Orders two tuples element by element like ValueTupleRoutines.Compare, with Comparer ordering each pair of elements, for example to sort text elements case-insensitively.結構相等Whether two tuples hold equal elements, position by position, with Comparer deciding each pair, for example to compare text elements case-insensitively.結構雜湊A hash code for a tuple in which each element is hashed by Comparer, to pair with ValueTupleRoutines.StructuralEquals using the same comparer, for example to treat text elements case-insensitively.轉為陣列Copies a tuple's elements into a new array in order, so they can be handled as a list.嘗試取得項Reads the element at a position in a tuple without throwing, telling you whether there was one.驗證元組Checks that a value is a tuple and throws if it is not, so a graph fails at a clear point instead of further on.

集合操作

新增清空Removes every element from the set, in place; the set itself stays usable and keeps its equality rule.複製集合An independent copy of the set with the same elements and the same equality rule, so that changing one does not change the other.比較器The equality rule the set uses to decide which elements are the same, for creating another set that behaves alike.包含Whether the set holds an element equal to Item, by the set's own equality rule; this is a fast lookup, however large the set.項數How many elements the set holds.建立集合A new, empty set that compares its elements by their default equality.差集The first operand's elements minus the second's, in a new set; both operands untouched.交集Only the elements both operands have, in a new set; both operands untouched.為空Whether the set holds no elements.是否為子集Whether every element of Set is also in Other, by the set's equality rule.是否為超集Whether every element of Other is also in Set, by the set's equality rule.是否有交集Whether the set and the other elements have at least one element in common, by the set's equality rule; false when either is empty.移除集合相等Whether the set and the other elements hold exactly the same elements, by the set's equality rule, ignoring order and any duplicates in Other.對稱差集The elements in exactly one of the two operands, in a new set; both operands untouched.轉為陣列Copies the set's elements into a new array, for when an indexed or ordered collection is needed.轉為列表Copies the set's elements into a new list, for when an indexed collection that can hold duplicates is needed.並集Every element in either operand, in a new set; both operands untouched.

快速儲存

物件操作

動態操作

集合查詢