5

オープン ソースの C# コードを読んだことがありますが、奇妙な文法がたくさんあります (私にとって)。

次のように、 thisキーワードを使用してメソッド引数を宣言します。

このオブジェクト @object

どういう意味ですか?

データ型の前にある「this」キーワードを削除すると、動作が異なりますか?

4

2 に答える 2

4

'this type name' 構文は、拡張メソッドに使用されます。

たとえば、メソッドを文字列に追加したい場合(つまり、"Hello World` を生成するUnCamelCaseことができます。次のように記述します。"HelloWorld".UnCamelCase()

public static string UnCamelCase(this string text)
{
    /*match any instances of a lower case character followed by an upper case 
     * one, and replace them with the same characters with a space between them*/
    return Regex.Replace(text, "([a-z])([A-Z])", "$1 $2");
}

this string text作業している文字列の特定のインスタンスを意味し、テキストはその識別子です。

@ 構文では、通常は予約されている変数名を使用できます。

于 2013-05-10T12:34:21.850 に答える