IEnumerable 인터페이스의 메소드인 GetEnumerator()를 구현하면 foreach문을 사용할 수 있는 객체를 만들 수 있다.

class MyEnumerator : IEnumerable
{
    private string[] arr = {"a", "b", "c", "d"};
        
    public IEnumerator GetEnumerator()
    {
        yield return arr[0]+"_postfix";
        yield return arr[1]+"_postfix";
        yield return arr[2]+"_postfix";
        yield break;
        yield return arr[3];
    }
}
    
class MainApp
{
    static void Main(string[] args)
    {
        MyEnumerator obj = new MyEnumerator();
        foreach (var item in obj)
        {
            Console.WriteLine(item);
        }
    }        
}

/*
a_postfix
b_postfix
c_postfix
*/

 

IEnumerator의 Current 프로퍼티, MoveNext() 메소드와 Reset() 메소드를 구현하여 반복문을 사용할 수 있는 객체를 만들 수 있다.

class MyList : IEnumerator
{
    private int[] arr;
    private int position = -1;

    public MyList(int length)
    {
        arr = new int[length];
    }

    public int this[int index]
    {
        get => arr[index];
        set
        {
            if (index >= arr.Length)
            {
                Array.Resize(ref arr, index + 1);
            }

            arr[index] = value;
        }
    }

    public object Current => arr[position];

    public bool MoveNext()
    {
        if (position == arr.Length - 1)
        {
            Reset();
            return false;
        }

        position++;
        return position < arr.Length;
    }

    public void Reset()
    {
        position = -1;
    }
}
    
class MainApp
{
    static void Main(string[] args)
    {
        MyList list = new MyList(0);
        for (int i = 0; i < 5; i++)
            list[i] = i;

        while (list.MoveNext())
        {
            Console.WriteLine(list.Current);
        }
    }
}

'c#' 카테고리의 다른 글

인덱서  (0) 2021.04.16
c# Auto properties  (0) 2021.04.15
가변길이 매개변수 params  (0) 2021.04.14
c# out 키워드 두 개 이상의 결과를 내보낼 때 사용  (0) 2021.04.13
c# ref return  (0) 2021.04.13

c#에서 인덱서는 클래스나 구조체의 인스턴스를 배열처럼 인덱싱할 수 있게 한다.

class MyIndexer
{
    private int[] arr;

    public MyIndexer(int length)
    {
        arr = new int[length];
    }

    public int this[int index]
    {
        get => arr[index];
        set
        {
            if (index >= arr.Length)
            {
                Array.Resize(ref arr, index+1);
            }
            arr[index] = value;  
        } 
    }

    public int Length => arr.Length;
}
    
class MainApp
{
    static void Main(string[] args)
    {
        MyIndexer indexer = new MyIndexer(0);
        for (int i = 0; i < 5; i++)
            indexer[i] = i;
        for (int i = 0; i < indexer.Length; i++)
        {
            Console.WriteLine(indexer[i]);
        }
    }        
}

'c#' 카테고리의 다른 글

IEnumerable과 IEnumerator  (0) 2021.04.16
c# Auto properties  (0) 2021.04.15
가변길이 매개변수 params  (0) 2021.04.14
c# out 키워드 두 개 이상의 결과를 내보낼 때 사용  (0) 2021.04.13
c# ref return  (0) 2021.04.13
class Member
{
    public string Name { get; set;} = "Anonymous";
    public DateTime Birth{ get; set; } = new DateTime(1, 1, 1);
}

class MainApp
{
    static void Main(string[] args)
    {
        Member member = new Member()
        {
            Birth = new DateTime(2000, 6, 28),
            Name = "홍길동"
        };

        Console.WriteLine($"Name : {member.Name}");
        Console.WriteLine($"birth : {member.Birth.ToShortDateString()}");
        /*
        Name : 홍길동
	birth : 2000-06-28
        */
    }
}

'c#' 카테고리의 다른 글

IEnumerable과 IEnumerator  (0) 2021.04.16
인덱서  (0) 2021.04.16
가변길이 매개변수 params  (0) 2021.04.14
c# out 키워드 두 개 이상의 결과를 내보낼 때 사용  (0) 2021.04.13
c# ref return  (0) 2021.04.13
internal class Program
{
    static int sum(int[] numbers)
    {
        int sum = 0;
        for (int  i= 0;  i< numbers.Length; i++)
        {
            sum += numbers[i];
        }
        return sum;
    }

    static int sumWithPrams(params int[] numbers)
    {
        int sum = 0;
        for (int  i= 0;  i< numbers.Length; i++)
        {
            sum += numbers[i];
        }
        return sum;
    }

    public static void Main(string[] args)
    {
        int result = sum(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
        Console.WriteLine(result);
        // 55
        result = sumWithPrams(1, 1, 1);
        Console.WriteLine(result);
        // 3
    }
}

'c#' 카테고리의 다른 글

인덱서  (0) 2021.04.16
c# Auto properties  (0) 2021.04.15
c# out 키워드 두 개 이상의 결과를 내보낼 때 사용  (0) 2021.04.13
c# ref return  (0) 2021.04.13
C# call by value, call by reference  (0) 2021.04.13
internal class Program
{
    static void Divide(int a, int b, out int quotiet, out int remainder)
    {
        quotiet = a/b;
        remainder = a % b;
    }
        
    public static void Main(string[] args)
    {
        int a = 20;
        int b = 3;
        int c;
        Divide(a, b, out c, out int d);

        Console.WriteLine("a:{0}, b:{1}, a/b:{2}, a%b:{3}",a,b,c,d);
        // a:20, b:3, a/b:6, a%b:2
    }
}

'c#' 카테고리의 다른 글

인덱서  (0) 2021.04.16
c# Auto properties  (0) 2021.04.15
가변길이 매개변수 params  (0) 2021.04.14
c# ref return  (0) 2021.04.13
C# call by value, call by reference  (0) 2021.04.13
class Product
{
    private int price = 100;

    public ref int GetPrice()
    {
        return ref price;
    }

    public void PrintPrice()
    {
        Console.WriteLine($"Price: {price}");
    }
}

internal class Program
{
    public static void Main(string[] args)
    {
        Product carrot = new Product();
        carrot.PrintPrice();
        ref int ref_local_price = ref carrot.GetPrice();
        int normal_local_price = carrot.GetPrice();
        ref_local_price = 10;
        Console.WriteLine(ref_local_price);
        Console.WriteLine(normal_local_price);
        carrot.PrintPrice();
    }
}

/*
Price: 100
10
100
Price: 10
*/

return: 값을 반환

ref return: 참조를 반환

'c#' 카테고리의 다른 글

인덱서  (0) 2021.04.16
c# Auto properties  (0) 2021.04.15
가변길이 매개변수 params  (0) 2021.04.14
c# out 키워드 두 개 이상의 결과를 내보낼 때 사용  (0) 2021.04.13
C# call by value, call by reference  (0) 2021.04.13
internal class Program
    {
        public static void SwapCallByValue(int a, int b)
        {
            int temp = b;
            b = a;
            a = temp;
        }
        
        public static void SwapCallByRef(ref int a, ref int b)
        {
            int temp = b;
            b = a;
            a = temp;
        }
        
        public static void Main(string[] args)
        {
            int x = 3;
            int y = 4;
            Console.WriteLine("x:{0}, y{1}",x, y);
            // SwapCallByValue(x, y);
            SwapCallByRef(ref x, ref y);
            Console.WriteLine("x:{0}, y{1}",x, y);
        }
    }

x: 3, y: 4

x: 4, y: 3

'c#' 카테고리의 다른 글

인덱서  (0) 2021.04.16
c# Auto properties  (0) 2021.04.15
가변길이 매개변수 params  (0) 2021.04.14
c# out 키워드 두 개 이상의 결과를 내보낼 때 사용  (0) 2021.04.13
c# ref return  (0) 2021.04.13

+ Recent posts