本文整理汇总了C#中Array.GetLength方法的典型用法代码示例。如果您正苦于以下问题:C# Array.GetLength方法的具体用法?C# Array.GetLength怎么用?C# Array.GetLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Array
的用法示例。
在下文中一共展示了Array.GetLength方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ArrayLength
static long ArrayLength(Array array, int i)
{
long n = 1;
for ( ; i < array.Rank; i++ )
n *= array.GetLength(i);
return n;
}
示例2: PrintValues
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
示例3: CreateBinaryArrayNode
private YamlNode CreateBinaryArrayNode(Array array)
{
var type = array.GetType();
var element = type.GetElementType();
if ( !TypeUtils.IsPureValueType(element) )
throw new InvalidOperationException(
"Can not serialize {0} as binary because it contains non-value-type(s).".DoFormat(type.FullName));
var elementSize = Marshal.SizeOf(element);
var binary = new byte[array.LongLength * elementSize];
int j = 0;
for ( int i = 0; i < array.Length; i++ ) {
IntPtr p = Marshal.UnsafeAddrOfPinnedArrayElement(array, i);
Marshal.Copy(p, binary, j, elementSize);
j += elementSize;
}
var dimension = "";
if ( array.Rank > 1 ) {
for ( int i = 0; i < array.Rank; i++ ) {
if ( dimension != "" )
dimension += ", ";
dimension += array.GetLength(i);
}
dimension = "[" + dimension + "]\r\n";
}
var result= str(TypeNameToYamlTag(type), dimension + Base64Encode(type, binary));
result.Properties["Don'tCareLineBreaks"] = "true";
return result;
}
示例4: CreateArrayNodeSub
private YamlNode CreateArrayNodeSub(Array array, int i, long[] indices)
{
var type= array.GetType();
var element = type.GetElementType();
var sequence = seq();
if ( i == 0 ) {
sequence.Tag = TypeNameToYamlTag(type);
AppendToAppeared(array, sequence);
}
if ( element.IsPrimitive || element.IsEnum || element == typeof(decimal) )
if ( array.Rank == 1 || ArrayLength(array, i+1) < 20 )
sequence.Properties["Compact"] = "true";
for ( indices[i] = 0; indices[i] < array.GetLength(i); indices[i]++ )
if ( i == array.Rank - 1 ) {
var n = ObjectToNode(array.GetValue(indices), type.GetElementType());
sequence.Add(n);
} else {
var s = CreateArrayNodeSub(array, i + 1, indices);
sequence.Add(s);
}
return sequence;
}
示例5: GetCoercedArrayLiteral
/// <summary>
/// Gets the array literal in arrayValue coercing TypeNode[] and EnumNode[] as needed.
/// </summary>
/// <param name="arrayType">A TypeNode representing the array type</param>
/// <param name="arrayValue">The value of the array literal to coerce</param>
/// <returns>An Array object that has been coerced to the appropriate runtime type</returns>
protected Array GetCoercedArrayLiteral(ArrayType arrayType, Array arrayValue)
{
if(arrayType == null)
return null;
if(arrayValue == null)
return null;
// Multi-dimensional arrays are not legal in attribute instances according section 17.1.3 of the C# 1.0 spec
if(arrayValue.Rank != 1)
return null;
TypeNode elemType = arrayType.ElementType;
if(elemType.typeCode != ElementType.ValueType && elemType.typeCode != ElementType.Class)
return arrayValue;
int arraySize = arrayValue.GetLength(0);
Type et = elemType.GetRuntimeType();
if(et == null)
return null;
Array val = Array.CreateInstance(et, arraySize);
for(int i = 0; i < arraySize; i++)
val.SetValue(this.GetCoercedLiteralValue(elemType, arrayValue.GetValue(i)), i);
return val;
}
示例6: CopyTo
public virtual void CopyTo (Array array, int index)
{
if (controls == null)
return;
// can't use controls.CopyTo (array, index);
// as we do not allocate it based on the true
// numbers of items we have in the collection
// so we must re-implement Array.CopyTo :(
if (array == null)
throw new ArgumentNullException ("array");
if (index + count > array.GetLowerBound (0) + array.GetLength (0))
throw new ArgumentException ();
if (array.Rank > 1)
throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
if (index < 0)
throw new ArgumentOutOfRangeException ("index", Locale.GetText ("Value has to be >= 0."));
for (int i=0; i < count; i++)
array.SetValue (controls [i], i + index);
}