欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

event demo

程序员文章站 2022-06-13 08:01:54
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; namespac... ......
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.diagnostics;

namespace consoleapp392
{
    class program
    {
        static void main(string[] args)
        {
            adult adult = new adult(18);
            adult.adultevent += adult_adultevent;
            adult.age = 20;
            console.readline();
        }

        private static void adult_adultevent(object sender, adultargs e)
        {
            string msg = string.empty;
            int newage = e.adultage;
            if(newage>=18)
            {
                msg = "adult";
            }
            else
            {
                msg = "adolescent";
            }
            console.writeline($"the newly updated age is {newage} and it's {msg} ");
        }
    }        

    public class adultargs
    {
        public int adultage { get; set; }
        public adultargs(int age)
        {
            adultage = age;
        }
    }

    public class adult
    {
        public event eventhandler<adultargs> adultevent;

        public adult(int adultage)
        {
            age = adultage;
        }

        private int agevalue;
        public int age
        {
            get
            {
                return agevalue;
            }
            set
            {
                if(value!=agevalue)
                {
                    agevalue = value;
                    raisepropertychanged(value);
                }
            }
        }

        private void raisepropertychanged(int value)
        {
            adultevent?.invoke(this, new adultargs(value));
        }
    }

}