fire events with automatic properties

CSharp · Console

/Program.cs

//https://stackoverflow.com/questions/4098059/can-you-fire-events-with-automatic-properties

using System;

public class Program
{
	public static void Main()
	{
		//You can get a stream from web service, and needs to have an instance of System.Drawing.Image
		// and getting metadata  
		
		  Console.WriteLine("fire events with automatic properties");
		  var user = new User();
         user.AgeChanged += (s,e) => Console.WriteLine("UserNamed changed to {0}",user.UserName);
	     user.UserName="Jack";
	}
	
}
public class User
	{
	    public event EventHandler AgeChanged;	  
	    private string _UserName;
	    public string UserName
	    {
	        get
	        {
	            return _UserName;
	        }
	         set  
	        {
	            _UserName = value;
	             AgeChanged(this,EventArgs.Empty);
				 	
	        }
	    }
	
	    protected virtual void OnAgeChanged(object sender ,EventArgs e)
	    {
	        if (AgeChanged != null)
	        {
	            AgeChanged(sender, e);
	        }
	    }
   }
 

;