Question about this bit of C# syntax...

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

LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
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.
0
Chinmay PatelChief Technical NinjaCommented:
And s and e are the parameters for whatever you are going to put between the following curly braces.
0
it_saigeDeveloperCommented:
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-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom KnowltonWeb developerAuthor Commented:
Okay.

Thank you!
0
Tom KnowltonWeb developerAuthor Commented:
Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.