Question about this bit of C# syntax...

Tom Knowlton
Tom Knowlton used Ask the Experts™
on
Not familiar with this syntax.

What does this line of code do?  What is (s, e)?  Looks like some sort of initialization for a collection of events?

public event EventHandler LoadingItemsStarted = (s, e) => { };

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Tom,

Those are lambda expressions i.e. Anonymous functions.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions

Regards,
Chinmay.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
And s and e are the parameters for whatever you are going to put between the following curly braces.
Commented:
It's a form of Lambda Expression.  Basically its a shorthanded delegate function bound to an event handler; e.g. -
using System;
using System.ComponentModel;

namespace EE_Q29120152
{
	class Program
	{
		static void Main(string[] args)
		{
			Person bob = new Person();
			bob.PropertyChanged += OnPropertyChanged;
			bob.Name = "Bob";
			bob.ID = 0;

			Console.ReadLine();
		}

		private static void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			Console.WriteLine("The property {0} has changed for {1}", e.PropertyName, sender);
		}
	}

	class Person : INotifyPropertyChanged
	{
		[NonSerialized]
		private static PropertyChangedEventHandler _propertyChanged = null;
		public event PropertyChangedEventHandler PropertyChanged = (s, e) =>
		{
			PropertyChangedEventHandler handler = _propertyChanged;
			if (handler != null)
				handler(s, e);
		};

		private int _id = -1;
		private string _name = string.Empty;

		public int ID
		{
			get { return _id; }
			set
			{
				if (!value.Equals(_id))
				{
					_id = value;
					PropertyChanged(this, new PropertyChangedEventArgs("ID"));
				}
			}
		}

		public string Name
		{
			get { return _name; }
			set
			{
				if (!value.Equals(_name))
				{
					_name = value;
					PropertyChanged(this, new PropertyChangedEventArgs("Name"));
				}
			}
		}

		public override string ToString()
		{
			return Name;
		}
	}
}

Open in new window

And this -
using System;
using System.ComponentModel;

namespace EE_Q29120152
{
	class Program
	{
		static void Main(string[] args)
		{
			Person bob = new Person();
			bob.PropertyChanged += OnPropertyChanged;
			bob.Name = "Bob";
			bob.ID = 0;

			Console.ReadLine();
		}

		private static void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			Console.WriteLine("The property {0} has changed for {1}", e.PropertyName, sender);
		}
	}

	class Person : INotifyPropertyChanged
	{
		[NonSerialized]
		private PropertyChangedEventHandler _propertyChanged;
		public event PropertyChangedEventHandler PropertyChanged
		{
			add { _propertyChanged += value; }
			remove { _propertyChanged -= value; }
		}

		protected virtual void OnPropertyChanged(object s, PropertyChangedEventArgs e)
		{
			PropertyChangedEventHandler handler = _propertyChanged;
			if (handler != null)
				handler(s, e);
		}

		private int _id = -1;
		private string _name = string.Empty;

		public int ID
		{
			get { return _id; }
			set
			{
				if (!value.Equals(_id))
				{
					_id = value;
					OnPropertyChanged(this, new PropertyChangedEventArgs("ID"));
				}
			}
		}

		public string Name
		{
			get { return _name; }
			set
			{
				if (!value.Equals(_name))
				{
					_name = value;
					OnPropertyChanged(this, new PropertyChangedEventArgs("Name"));
				}
			}
		}

		public override string ToString()
		{
			return Name;
		}
	}
}

Open in new window

Produce the same output -Capture.PNG-saige-
Tom KnowltonWeb developer

Author

Commented:
Okay.

Thank you!
Tom KnowltonWeb developer

Author

Commented:
Thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial