티스토리 뷰

190730 - 4 - 클래스

 
base : 부모 클래스
sealed : 상속 불가능
 
 
확장 매서드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyExten;
namespace MyExten
{
    public static class IntExtension
    {
        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;
        }
        public static string Append(this string str, string addStr)
        {
            return str + addStr;
        }
    }
}
 
namespace ExtentionMethod
{
    class Program
    {
    
        static void Main(string[] args)
        {
            Console.WriteLine($"3^2 : {3.Square()}");
            Console.WriteLine($"3^4 : {3.Power(4)}");
            Console.WriteLine($"3^10 : {3.Power(10)}");
            Console.WriteLine($"3^10 : {3.Power(10)}".Append(" 붙임"));
        }
    }
}
3^2 : 9
3^4 : 81
3^10 : 59049
3^10 : 59049 붙임
 
튜플 설치
Install-Package "System.ValueTuple"
 
튜플 사용
static void Main(string[] args)
{
    var a = ("슈퍼맨", 9999);
    Console.WriteLine($"{a.Item1}, {a.Item2}");
 
    var b = (Name: "인간", Age: 25);
    Console.WriteLine($"{b.Name}, {b.Age}");
 
    var (name, age) = b;
    Console.WriteLine($"{name}, {age}");
 
    b = a;
    Console.WriteLine($"{b.Name}, {b.Age}");
}
댓글
최근에 올라온 글
최근에 달린 댓글
네이버 이웃추가
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함