Programming」カテゴリーアーカイブ

プログラミング全般に関するカテゴリーです。

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

GitHubをプロキシ環境下で使う方法

.gitconfigで設定する方法

以下のコマンドを実行します。

$ git config --global http.https://github.com.proxy http://123.456.789.012:1234

もしくは、$HOME/.gitconfigに以下の内容を追加してください。

[http "https://github.com"]
proxy = 123.456.789.012:1234

※プロキシサーバーへのアクセスにユーザー名やパスワードが必要な場合はhttp://username:password@123.456.789.012:1234のような形で設定できます。

.ssh/configで設定する方法

$HOME/.ssh/configに以下の内容を追加します。

Host github.com
ProxyCommand connect -H 123.456.789.012:1234 %h %p

C#で数値を文字列に変換したり文字列を数値に変換したりする方法

C#で数値を文字列に変換したり文字列を数値に変換したりする方法のまとめ。

数値 ⇒ 文字列の変換

数値から文字列への変換にはToString()が使えます。

サンプルコード

using System;

public class Example1
{
  public static void Main()
  {
    int value1 = 123;
    float value2 = 456.78f;

    Console.WriteLine("value1 = " + value1.ToString());
    Console.WriteLine("value2 = " + value2.ToString());
  }
}

実行結果

value1 = 123
value2 = 456.78

文字列 ⇒ 数値の変換

文字列から数値への変換はParse()が使えます。
例外を発生させたくない場合はTryParse()も使えます。

サンプルコード

using System;

public class Example2
{
  public static void Main()
  {
    try
    {
      int value1 = int.Parse("123");
      float value2 = float.Parse("456.78");

      Console.WriteLine("value1 = " + value1);
      Console.WriteLine("value2 = " + value2);
    }
    catch (FormatException)
    {
    }
  }
}

実行結果

value1 = 123
value2 = 456.78

文字列 ⇒ 数値の変換(TryParse編)

サンプルコード

using System;

public class Example3
{
  public static void Main()
  {
    if (int.TryParse("123", out int value1))
    {
      Console.WriteLine("value1 = " + value1);
    }
    if (float.TryParse("456.78", out float value2))
    {
      Console.WriteLine("value2 = " + value2);
    }
  }
}

実行結果

value1 = 123
value2 = 456.78

リンク

How to convert a string to a number – C# Programming Guide | Microsoft Docs
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number