class Program
{
static void Main(string[] args)
{
int num = 2;
// 2の2乗
Console.WriteLine(num.Square());
// 2の3乗
Console.WriteLine(num.Square().Square());
// 2の4乗
Console.WriteLine(num.Square().Square().Square());
Console.WriteLine();
}
}
▸ 実行ボタンで結果を表示
Source収録記事
この snippet は記事の「そもそも拡張メソッドとは?」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。
同じ記事から
8 件public static class IntExtenssionMethod { public static int Square(this int num) {未収録
そもそも拡張メソッドとは?
#ada51f9d3b8b
public static class StringExtenssionMethod { public static string Decoration(this string str) {未収録
そもそも拡張メソッドとは?
#485517d8bf92
string str = "Hello World"; Console.WriteLine(str.Decoration()); Console.ReadLine();
▶ 実行可
そもそも拡張メソッドとは?
#75178f10b7fa
namespace InterfaceExtenssionMethod { public interface IAnimal {未収録
Interfaceに拡張メソッドを入れるとデフォルト実装できる
#9d80b8e1e416
public static class AnimalExtenssionMethod { public static void Roar(this IAnimal animal) {
▶ 実行可
Interfaceに拡張メソッドを入れるとデフォルト実装できる
#c0e9f3bf46f1
public class Dog : IAnimal { public string RoarSound => "わんわん"; }未収録
Interfaceに拡張メソッドを入れるとデフォルト実装できる
#bbdeb9f5ad88
