C#」カテゴリーアーカイブ

プログラミング言語「C#」に関するカテゴリーです。

C#で配列のサイズを調べる方法

配列のサイズを調べる方法

Lengthを使って配列のサイズを調べることができます。

int[] array1 = new int[]{ 1, 2, 3, 4, 5 };

Debug.Log($"Length = {array1.Length}");

実行結果

Length = 5

ジャグ配列の場合

int[][] array2 = new int[][]{
  new int[] { 1, 2, 3 },
  new int[] { 4, 5, 6, 7 }
};

Debug.Log($"Length = {array2.Length}");
Debug.Log($"Length = {array2[0].Length}");
Debug.Log($"Length = {array2[1].Length}");

実行結果

Length = 2
Length = 3
Length = 4

多次元配列の場合

多次元配列の場合はGetLengthを使って調べられます。

int[,] array3 = new int[,]{
  { 1, 2, 3 },
  { 4, 5, 6 }
};

Debug.Log($"Length = {array3.GetLength(0)}");
Debug.Log($"Length = {array3.GetLength(1)}");

実行結果

Length = 2
Length = 3

C#でoperator[]のオーバーロード

operator[]のオーバーロード

C#で自作クラスにoperator[]を追加したい場合は次のような感じで実装できます。

class Example
{
  int[] _ar = new int[10];                                                                       
  
  public int this[int i]
  {
    get => _ar[i];                                                                        
    set => _ar[i] = value;                                                                       
  }
}

Exampleクラスを使う方は次のような感じです。

var example = new Example();

example[2] = 345;

Debug.Log($"example[2] = {example[2]}");

実行結果

example[2] = 345

多次元配列

多次元配列のような感じで使いたい場合も次のような感じで実装できます。

class Example2
{
  int[,] _ar = new int[10, 10];

  public int this[int i, int j]
  {
    get => _ar[i, j];
    set => _ar[i, j] = value;
  }
}

Compiler Error CS0720

static classoperator[]を実装しようとするとCS0720というエラーになるようです。気を付けてください。

/* static */ class Example3
{
  public int this[int i]
  {
    get => 0;
  }
}

※配列の範囲外にアクセスしようとするとIndexOutOfRangeExceptionという例外が発生します。実際に使う場合は注意してください。

C#でソースコードの行番号やファイル名を取得する方法

C++言語では__LINE____FILE__のマクロでソースコードの行番号やファイル名を取得することができます。C#でもこれと同じようなことをしたいと思って方法を調べてみました。

StackFrameを使う方法

using System;
using System.Diagnostics;

public class Example1
{
  public static void Main()
  {
    StackFrame stackFrame = new StackFrame(true);

    int fileLineNumber = stackFrame.GetFileLineNumber();
    string fileName = stackFrame.GetFileName();

    Console.WriteLine($"FileLineNumber: {fileLineNumber}");
    Console.WriteLine($"FileName: {fileName}");
  }
}

実行結果

FileLineNumber: 8
FileName: C:\work\example1.cs

CallerLineNumber/CallerFilePathを使う方法

using System;
using System.Runtime.CompilerServices;

public class Example2
{
  public static void Main()
  {
    test();
  }

  static void test([CallerLineNumber] int lineNumber = 0, [CallerFilePath] string filePath = null)
  {
    Console.WriteLine($"LineNumber: {lineNumber}");
    Console.WriteLine($"FilePath: {filePath}");
  }
}

実行結果

LineNumber: 8
FilePath: C:\work\example2.cs

ファイル名のみを取得したい場合

どちらの方法を使っても取得できるのはファイル名ではなく、ファイルパスになっています。ファイル名のみを取得したい場合はGetFileNameを使うと便利です。

string fileName = System.IO.Path.GetFileName(filePath);

リンク

StackFrame Class (System.Diagnostics) | Microsoft Docs
https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stackframe?view=net-6.0

System.Runtime.CompilerServices Namespace | Microsoft Docs
https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices?view=net-6.0