250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • ใ…ฃใ„ท
  • MySQL
  • ์œ„์ž„
  • ์ปดํฌ์ง€์…˜

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

C#/์ด๊ฒƒ์ด C#์ด๋‹ค

30. ํ™•์žฅ ๋ฉ”์†Œ๋“œ

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

'C# > ์ด๊ฒƒ์ด C#์ด๋‹ค' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

32. ํŠœํ”Œ  (0) 2022.02.14
31. ๊ตฌ์กฐ์ฒด, ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ ๊ตฌ์กฐ์ฒด(readonly struct)  (0) 2022.02.14
29. ๋ถ„ํ•  ํด๋ž˜์Šค  (0) 2022.02.11
28. ์ค‘์ฒฉ ํด๋ž˜์Šค  (0) 2022.02.11
27. ์ฝ๊ธฐ ์ „์šฉ ํ•„๋“œ, readonly  (0) 2022.02.11
    'C#/์ด๊ฒƒ์ด C#์ด๋‹ค' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • 32. ํŠœํ”Œ
    • 31. ๊ตฌ์กฐ์ฒด, ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ ๊ตฌ์กฐ์ฒด(readonly struct)
    • 29. ๋ถ„ํ•  ํด๋ž˜์Šค
    • 28. ์ค‘์ฒฉ ํด๋ž˜์Šค
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”