2019年7月9日 星期二

[ASP.NET]簡易設計擴充方法範例




using System;
using System.Collections.Generic;
using System.Linq;

namespace RichieCS
{
    public static class RichieCSExtension{
        public static string SayHello(this string original){
            return "Hello~" + original ;
        }
        public static string CalBornYear(this int original){
            return string.Format( "You are born in {0}", DateTime.Now.Year - original );
        }
    }
   
    public class Program
    {
        public static void Main(string[] args)
        {
           
            var myname = "Richie";
            var myage = 18;
            Console.WriteLine(myname.SayHello());
            Console.WriteLine(myage.CalBornYear());
        }
    }
}