数据类型
稳定列表、字典、集合,以及值在不同类型之间的转换
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.
快速存储
对象操作
创建对象A new, empty object that holds no data and equals nothing but itself, useful as a unique marker or as something to lock on.相等Whether two values are equal, using the first value's own idea of equality (so two texts with the same characters are equal).获取哈希码The value's hash code, the number dictionaries and sets use to file it.获取类型The actual type of the value at run time, which can be more specific than the type of the wire it arrived on.逻辑相等Whether Target considers itself equal to Obj, by whatever equality its type defines (which may be custom).浅复制A shallow copy of the value: a new object of the same type whose fields hold the same values as the original's, so any objects it refers to (lists, for example) are shared with the original rather than copied.引用相等Whether two inputs are the very same object, not merely equal in content; two nulls count as the same.转为文本The value's text form, as its type defines it; a type that defines none gives its full type name.