C#/이것이 C#이닀

30. ν™•μž₯ λ©”μ†Œλ“œ

Rainbow🌈Coder 2022. 2. 14. 10:40
728x90

ν™•μž₯ λ©”μ†Œλ“œλŠ” κΈ°μ‘΄ 클래슀의 κΈ°λŠ₯을 ν™•μž₯ν•˜λŠ” 기법이닀.

기반 클래슀λ₯Ό λ¬Όλ €λ°›μ•„ νŒŒμƒ 클래슀λ₯Ό λ§Œλ“  λ’€ 여기에 ν•„λ“œλ‚˜ λ©”μ†Œλ“œλ₯Ό μΆ”κ°€ν•˜λŠ” μƒμ†κ³ΌλŠ” λ‹€λ₯΄λ‹€.

ν™•μž₯λ©”μ†Œλ“œλŠ” 'κΈ°μ‘΄ 클래슀'의 κΈ°λŠ₯을 ν™•μž₯ν•œλ‹€.

ν™•μž₯ λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•˜λ©΄ string ν΄λž˜μŠ€μ— λ¬Έμžμ—΄μ„ λ’€μ§‘λŠ” κΈ°λŠ₯을 넣을 μˆ˜λ„ 있고,

int ν˜•μ‹μ— μ ‘κ³± μ—°μ‚° κΈ°λŠ₯을 넣을 μˆ˜λ„ μžˆλ‹€. 

 

<예제>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using MyExtension; //ν™•μž₯ λ©”μ†Œλ“œλ₯Ό λ‹΄λŠ” ν΄λž˜μŠ€μ™€ λ„€μž„μŠ€νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•œλ‹€.

namespace MyExtension
{
    public static class IntegerExtension
    {
        public static int Square(this int myInt)
        {
            return myInt * myInt;
        }
        public static int Power(this int myInt, int exponent)
        {
            int result = myInt;
            for (int i = 1; i < exponent; i++)
                result = result * myInt;

            return result;
        }
    }
}
namespace ExtensionMethod
{
    class MainApp
    {
        static void Main(string[] args)
        {
            WriteLine($"3^2 : {3.Square()}"); //3^2 : 9
            WriteLine($"3^4 : {3.Power(4)}"); //3^4 : 81
            WriteLine($"2^10 : {2.Power(10)}"); //2^10 : 1024
            
            //마치 Power()κ°€ μ›λž˜λΆ€ν„° int ν˜•μ‹μ˜ λ©”μ†Œλ“œμ˜€λ˜ κ²ƒμ²˜λŸΌ μ‚¬μš©ν•  수 μžˆλ‹€.
        }
    }
}

<예제 2>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using MyExtension;

namespace MyExtension
{
    public static class StringExtension
    {
        public static string Append(this string myString, string appendStr)
        {
            string thisStr = myString;
            return thisStr += appendStr;
        }
    }
}
namespace ExtensionMethod
{
    class MainApp
    {
        static void Main(string[] args)
        {
            string hello = "Hello";
            WriteLine(hello.Append(", World!")); //Hello, World!
        }
    }
}

ν™•μž₯λ©”μ†Œλ“œλ₯Ό μ„ μ–Έν•˜λŠ” 방법은 λ‹€μŒκ³Ό κ°™λ‹€.

 

(1) λ©”μ†Œλ“œλ₯Ό μ„ μ–Έν•˜λ˜, static ν•œμ •μžλ‘œ μˆ˜μ‹ν•΄μ•Ό ν•œλ‹€.

(2) λ©”μ†Œλ“œμ˜ 첫번째 λ§€κ°œλ³€μˆ˜λŠ” λ°˜λ“œμ‹œ this ν‚€μ›Œλ“œμ™€ ν•¨κ»˜ ν™•μž₯ν•˜κ³ μž ν•˜λŠ” 클래슀(ν˜•μ‹)의 μΈμŠ€ν„΄μŠ€μ—¬μ•Ό ν•œλ‹€.

(3) (2)뒀에 λ”°λΌμ˜€λŠ” λ§€κ°œλ³€μˆ˜ λͺ©λ‘μ΄ μ‹€μ œλ‘œ ν™•μž₯ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•  λ•Œ μž…λ ₯λ˜λŠ” λ§€κ°œλ³€μˆ˜μ΄λ‹€.

(λ©”μ†Œλ“œλŠ” 클래슀 없이 선언될 수 μ—†μœΌλ―€λ‘œ 클래슀λ₯Ό ν•˜λ‚˜ μ„ μ–Έν•˜κ³ , κ·Έ μ•ˆμ— ν™•μž₯ λ©”μ†Œλ“œλ₯Ό μ„ μ–Έν•œλ‹€.

μ΄λ•Œ μ„ μ–Έν•˜λŠ” ν΄λž˜μŠ€λ„ μ—­μ‹œ static ν•œμ •μžλ‘œ μˆ˜μ‹ν•΄μ•Ό ν•œλ‹€.)

namespace λ„€μž„μŠ€νŽ˜μ΄μŠ€μ΄λ¦„
{
	public static class ν΄λž˜μŠ€μ΄λ¦„
    {                                       //λŒ€μƒν˜•μ‹: ν™•μž₯ν•˜κ³ μž ν•˜λŠ” 클래슀 λ˜λŠ” ν˜•μ‹
    	public static λ°˜ν™˜_ν˜•μ‹ λ©”μ†Œλ“œμ΄λ¦„(this λŒ€μƒν˜•μ‹ μ‹λ³„μž, λ§€κ°œλ³€μˆ˜λͺ©λ‘)
        {
        	//
        }
    }
}

        public static int Power(this int myInt, int exponent)
        {
            int result = myInt;
            for (int i = 1; i < exponent; i++)
                result = result * myInt;

            return result;
        }

728x90