C#の配列の指定された次元の要素の総数
配列の指定された次元の要素の総数を取得するには、コードは次のとおりです-
例
using System;
public class Demo {
public static void Main() {
string[] products = new string[] { "Andy", "Mark", "Gary", "Andre"};
Console.WriteLine("One or more name begins with 'A'? = {0}",
Array.Exists(products, ele => ele.StartsWith("A")));
Console.WriteLine("Is the array having fixed size? = " + products.IsFixedSize);
Console.WriteLine("Is the array read only? = " + products.IsReadOnly);
Console.WriteLine("Is the array synchronized? = " + products.IsSynchronized);
Console.WriteLine("Index of the 1st element = "+products.GetLowerBound(0));
Console.WriteLine("Index of the last element = "+products.GetUpperBound(0));
Console.WriteLine("GetLongLength(0) = "+products.GetLongLength(0));
}
} 出力
これにより、次の出力が生成されます-
One or more name begins with 'A'? = True Is the array having fixed size? = True Is the array read only? = False Is the array synchronized? = False Index of the 1st element = 0 Index of the last element = 3 GetLongLength(0) = 4
別の例を見てみましょう-
例
using System;
public class Demo {
public static void Main() {
string[,, ] stdNames = new string[,, ] {
{ { "John", "Gary" },
{ "Jacob", "Harry" } },
{ { "Kevin", "Ryan" },
{ "David", "Mark" } },};
Console.WriteLine("Is the array having fixed size? = " + stdNames.IsFixedSize);
Console.WriteLine("Is the array read only? = " + stdNames.IsReadOnly);
Console.WriteLine("Is the array synchronized? = " + stdNames.IsSynchronized);
Console.WriteLine("Index of the 1st element = "+stdNames.GetLowerBound(0));
Console.WriteLine("Index of the last element = "+stdNames.GetUpperBound(0));
Console.WriteLine("GetLongLength(0) = "+stdNames.GetLongLength(0));
Console.WriteLine("GetLongLength(0) = "+stdNames.GetLongLength(1));
}
} 出力
これにより、次の出力が生成されます-
Is the array having fixed size? = True Is the array read only? = False Is the array synchronized? = False Index of the 1st element = 0 Index of the last element = 1 GetLongLength(0) = 2 GetLongLength(0) = 2
-
配列にC#で指定された条件に一致する要素が含まれているかどうかを確認します
配列に特定の条件に一致する要素が含まれているかどうかを確認するには、C#でStartsWith()メソッドを使用できます- 例 using System; public class Demo { public static void Main(){ string[] products = { "Electronics", "Accessories", "Clothing", "Toys", "Clothing", "F
-
配列の最初の要素をスキップするC#プログラム
配列内の多数の要素をスキップする場合は、C#でSkip()メソッドを使用します。 以下が私たちの配列だとしましょう- int[] arr = { 24, 40, 55, 62, 70, 82, 89, 93, 98 }; 最初の4つの要素をスキップします- var ele = arr.Skip(4); 完全な例を見てみましょう- 例 using System.IO; using System; using System.Linq; public class Demo { public static void Main() { &nbs