如何判断三个值在 IEnumerable<String>里面都存在
时间: 2024-02-05 09:10:33 浏览: 100
可以使用 LINQ 的 `All` 方法,它接受一个 lambda 表达式作为参数,用于判断集合中的所有元素是否都满足某个条件。
示例代码:
```csharp
IEnumerable<string> strings = new[] { "foo", "bar", "baz" };
string[] valuesToCheck = { "foo", "bar", "qux" };
bool allExist = valuesToCheck.All(value => strings.Contains(value));
```
在这个示例中,`valuesToCheck` 数组中的所有值都会被检查,如果它们都存在于 `strings` 集合中,则 `allExist` 变量的值为 `true`,否则为 `false`。
相关问题
IEnumerable<float> ElementAt
### C# 中 `IEnumerable<float>` 的 `ElementAt` 方法使用
在C#中,`IEnumerable<T>` 接口表示一个可以枚举的对象集合。对于浮点数类型的序列 (`IEnumerable<float>`),可以通过 LINQ 提供的方法来访问特定索引处的元素。
#### 使用 `ElementAt` 访问指定位置的元素
`ElementAt<int>(index)` 是LINQ中的扩展方法之一,用于获取泛型列表或其他实现了 `IEnumerable<T>` 接口的数据结构中位于给定索引处的项。如果尝试访问超出范围的位置,则会抛出异常[^1]。
下面是一个简单的例子展示如何利用此功能:
```csharp
using System;
using System.Linq;
class Program {
static void Main(string[] args){
// 创建一个包含多个float数值的数组并转换成IEnumerable<float>
var numbers = new float[]{ 1.5f, 2.3f, 3.7f }.AsEnumerable();
try{
// 获取第二个元素(注意:索引是从零开始计数)
Console.WriteLine($"The element at index 1 is {numbers.ElementAt(1)}");
// 尝试获取不存在的第五个元素
Console.WriteLine(numbers.ElementAt(4));
}
catch (ArgumentOutOfRangeException e){
Console.WriteLine(e.Message);
}
}
}
```
这段程序首先定义了一个由三个浮点数组成的数组,并将其转化为 `IEnumerable<float>` 类型以便后续操作。接着通过调用 `ElementAt()` 来取得下标为1(即第二位)上的值;当试图读取越界的第四个元素时,捕获到了相应的错误信息并打印出来。
为了安全起见,在实际应用中应当先确认所需索引确实存在于当前集合内再执行此类查询动作,或者考虑采用更健壮的方式如 `ElementAtOrDefault()` ,它会在遇到非法输入时不引发异常而是返回默认值。
class CubeModel : IBufferSource { private const float halfLength = 0.5f; private static readonly vec3[] positions = new vec3[] { new vec3(+halfLength, +halfLength, +halfLength), // 0 new vec3(+halfLength, +halfLength, -halfLength), // 1 new vec3(+halfLength, -halfLength, +halfLength), // 2 new vec3(+halfLength, -halfLength, -halfLength), // 3 new vec3(-halfLength, +halfLength, +halfLength), // 4 new vec3(-halfLength, +halfLength, -halfLength), // 5 new vec3(-halfLength, -halfLength, +halfLength), // 6 new vec3(-halfLength, -halfLength, -halfLength), // 7 }; private static readonly uint[] indexes = new uint[] { 0, 2, 1, 1, 2, 3, // +X faces. 0, 1, 5, 0, 5, 4, // +Y faces. 0, 4, 2, 2, 4, 6, // +Z faces. 7, 6, 4, 7, 4, 5, // -X faces. 7, 5, 3, 3, 5, 1, // -Z faces. 7, 3, 2, 7, 2, 6, // -Y faces. }; public const string strPosition = "position"; private VertexBuffer positionBuffer; // array in GPU side. private IDrawCommand drawCommand; #region IBufferSource 成员 public IEnumerable<VertexBuffer> GetVertexAttribute(string bufferName) { if (strPosition == bufferName) // requiring position buffer. { if (this.positionBuffer == null) { // transform managed array to vertex buffer. this.positionBuffer = positions.GenVertexBuffer( VBOConfig.Vec3, // mapping to 'in vec3 someVar;' in vertex shader. BufferUsage.StaticDraw); // GL_STATIC_DRAW. } yield return this.positionBuffer; } else { throw new ArgumentException("bufferName"); } } public IEnumerable<IDrawCommand> GetDrawCommand() { if (this.drawCommand == null) { // indexes in GPU side. IndexBuffer indexBuffer = indexes.GenIndexBuffer(BufferUsage.StaticDraw); this.drawCommand = new DrawElementsCmd(indexBuffer, DrawMode.Triangles); // GL_TRIANGLES. } yield return this.drawCommand; } #endregion }逐行分段解释上述程序
这段程序定义了一个名为 CubeModel 的类,实现了 IBufferSource 接口。
```c#
class CubeModel : IBufferSource {
```
类中定义了一个常量 halfLength,值为 0.5。
```c#
private const float halfLength = 0.5f;
```
类中定义了一个静态只读的 vec3 数组 positions,包含了八个顶点的位置。
```c#
private static readonly vec3[] positions = new vec3[] {
new vec3(+halfLength, +halfLength, +halfLength), // 0
new vec3(+halfLength, +halfLength, -halfLength), // 1
new vec3(+halfLength, -halfLength, +halfLength), // 2
new vec3(+halfLength, -halfLength, -halfLength), // 3
new vec3(-halfLength, +halfLength, +halfLength), // 4
new vec3(-halfLength, +halfLength, -halfLength), // 5
new vec3(-halfLength, -halfLength, +halfLength), // 6
new vec3(-halfLength, -halfLength, -halfLength), // 7
};
```
类中定义了一个静态只读的 uint 数组 indexes,包含了组成立方体的三角形面的顶点索引。
```c#
private static readonly uint[] indexes = new uint[] {
0, 2, 1, 1, 2, 3, // +X faces.
0, 1, 5, 0, 5, 4, // +Y faces.
0, 4, 2, 2, 4, 6, // +Z faces.
7, 6, 4, 7, 4, 5, // -X faces.
7, 5, 3, 3, 5, 1, // -Z faces.
7, 3, 2, 7, 2, 6, // -Y faces.
};
```
类中定义了一个常量 strPosition,值为 "position"。
```c#
public const string strPosition = "position";
```
类中定义了一个私有的 VertexBuffer 类型的 positionBuffer,用于在 GPU 中存储顶点位置数据。
```c#
private VertexBuffer positionBuffer; // array in GPU side.
```
类中定义了一个私有的 IDrawCommand 类型的 drawCommand,用于在 GPU 中绘制立方体。
```c#
private IDrawCommand drawCommand;
```
接下来是实现 IBufferSource 接口的方法。
```c#
#region IBufferSource 成员
public IEnumerable<VertexBuffer> GetVertexAttribute(string bufferName) {
if (strPosition == bufferName) // requiring position buffer.
{
if (this.positionBuffer == null) {
// transform managed array to vertex buffer.
this.positionBuffer = positions.GenVertexBuffer(
VBOConfig.Vec3, // mapping to 'in vec3 someVar;' in vertex shader.
BufferUsage.StaticDraw); // GL_STATIC_DRAW.
}
yield return this.positionBuffer;
}
else {
throw new ArgumentException("bufferName");
}
}
```
GetVertexAttribute 方法返回一个可枚举的 VertexBuffer 集合,根据输入的 bufferName 参数选择返回的 VertexBuffer。如果 bufferName 的值等于 strPosition 常量的值,则返回存储顶点位置数据的 positionBuffer;否则抛出 ArgumentException 异常。
```c#
public IEnumerable<IDrawCommand> GetDrawCommand() {
if (this.drawCommand == null) {
// indexes in GPU side.
IndexBuffer indexBuffer = indexes.GenIndexBuffer(BufferUsage.StaticDraw);
this.drawCommand = new DrawElementsCmd(indexBuffer, DrawMode.Triangles); // GL_TRIANGLES.
}
yield return this.drawCommand;
}
#endregion
```
GetDrawCommand 方法返回一个可枚举的 IDrawCommand 集合,返回用于绘制立方体的 IDrawCommand。如果 drawCommand 为空,则创建一个 IDrawCommand 对象,存储绘制立方体所需的索引数据,并将其赋值给 drawCommand 变量。然后返回该 drawCommand 变量。
阅读全文
相关推荐
















