티스토리 툴바


'scs'에 해당되는 글 2건

  1. 2010/03/30 C# 4.0 - 옵션 매개변수와 매개변수 지정
  2. 2010/03/24 VB의 With를 흉내내기
분류없음2010/03/30 13:56
public int Add(int a, int b = 0, int c = 0)

Add(10)
Add(10,20)
Add(10,20,30)

이런 형태도 가능하고

Add(c:30, b:20, a:10)

이런 형태도 가능하다.

옵션 매개변수가 가능하니까

public int Add(int a)
{
  return Add(a, 0, 0);
}

이런 형태로 함수 오버로딩을 작성할 필요가 사라졌다.

저작자 표시 비영리 동일 조건 변경 허락
Posted by nekolatte nekolatte
TAG PRG, scs
분류없음2010/03/24 14:12
VB의 With 키워드를 흉내내기.
흥미있는 기사가 있길래 살짝 가져다가 예제 코드를 작성해봤다.
http://msmvps.com/blogs/omar/archive/2010/03/21/c-with-keyword-equivalent.aspx

// With 키워드 흉내내기
using System;
using System.Collections.Generic;

static class Helper
{
  public static void Use(this T item, Action work)
  {
    work(item);
  }
}

class Program
{

  public int Min{get;set;}
  public int Max{get;set;}
  public string Name{get;set;}

  static void Main()
  {
    Program p = new Program();
    p.Use( a=>
    {
      a.Min = 10;
      a.Max = 100;
      a.Name = "Engine";
    });

    Console.WriteLine("Min: " + p.Min);
    Console.WriteLine("Max: " + p.Max);
    Console.WriteLine("Name: " + p.Name);
  }
}

ps. 켁... 티스토리는 <T>를 태그로 인식해서 <t>로 바꿔버리고, </t>가 자동으로 들어간다.. 헐헐..
저작자 표시 비영리 동일 조건 변경 허락
Posted by nekolatte nekolatte
TAG PRG, scs