Updated April 7, 2023
Introduction to C# object sender
The C# Object sender is one of the argument, and it’s a parameter for creating the reference of the object which has been raised for the events that are used for to respond the handler to mapping the correct object but in case of static parameters or events, the value will be null with the help of EventArgs class we can pass the parameters to the method, so the EventArgs class and its constructor is used for creating the objects these classes are coming from the default frameworks so that it utilizes wherever the user needs on the application.
Syntax:
In C#, an object is the root and parent class of the application; when we want to utilize the specific class methods, keywords, and variables to create objects for the specific class, it will be achieved.
using System;
using System. Web;
Access modifiers class class name {
Access modifiers return type function_name(object reference name(sender), EventArgs reference name)
{
--some C# code logics depends upon the user requirement-----
}
}
The above codes are the basic syntax for utilizing and accessing the class objects from one place to another place. It looks like EventArgs and the handler to handle the created objects.
How does object sender work in C#?
In the general object, the sender is one of the parameters in the C# language, and also, it is used to create the instance of the object, which is raised by the specific events on the application. That event is handled using the Eventhandler mechanism that is mostly handled and responsible for creating the objects. But when we used some keywords like static, dynamic, the values are varied from each other if we use static events, the parameter values are always null. The event handlers are one of the methods that can be mostly used for executing the objects based on the client request; then, the response will be thrown on the specific events that will be occurring on the application.
Due to the object sender reference, the event will be triggered and performed, so the sender is one of the main control that can be used to activate the events. In UI button is one of the events and is mostly used for performing the user operations and actions on the backend. Once we clicked the sender button, the event will be triggered, and operations are performed based on the user requirement; the client’s data will be validated from both the front and back end.
Examples
Below are the different examples of C# object sender:
Example #1
using System;
public delegate void demo(object snd, eventargs er);
public delegate void demo1(object snd, eventargs er);
public delegate void demo2(object snd, eventargs er);
class examp
{
public const string first = "Welcome To My Domain";
public void meth1(object snd, eventargs er)
{
Console.WriteLine("Thank you user your first example program is started ", er.first);
}
public void meth2(object snd, eventargs er)
{
Console.WriteLine("Your code is currently debugged and in-progress", er.first);
}
public void meth3(object snd, eventargs er)
{
Console.WriteLine("Your code is successfully completed", er.first);
}
public examp(examp1 exm)
{
demo d1 = new demo(meth1);
demo1 d2 = new demo1(meth2);
demo2 d3 = new demo2(meth3);
exm.vars1 += d1;
exm.vars2 += d2;
exm.vars3 += d3;
}
}
class examp1
{
public event demo vars1;
public event demo1 vars2;
public event demo2 vars3;
public void third(eventargs er)
{
if (vars1 != null)
{
vars1(this, er);
}
}
public void four(eventargs er)
{
if (vars2 != null)
{
vars2(this, er);
}
}
public void five(eventargs er)
{
if (vars3 != null)
{
vars3(this, er);
}
}
}
public class eventargs : EventArgs
{
public string first;
}
public class second
{
private void seven(object snd, EventArgs er)
{
Console.WriteLine("Welcome To My Domain, please add your details");
}
public static void Main()
{
examp1 exm = new examp1();
examp exam = new examp(exm);
second s= new second();
eventargs e1 = new eventargs();
eventargs e2 = new eventargs();
e1.first = "Your first event is started and triggered";
e2.first = "Your second event is started and triggered";
s.seven("Welcome To My Domain, Thank you users ",e1);
exm.third(e1);
exm.four(e2);
}
}
Output:
In the first example, we used the same object sender and event handler mechanism for creating and sending the object reference and utilized with the default methods. We can create the two different events, and by using their instance, we can call these events and created the separate instance printed on the output console.
Example #2
using System;
using System.Collections;
public delegate void EventHandler(object snd, EventArgs er);
public class first: ArrayList
{
public event EventHandler vars;
public virtual void OnChanged(EventArgs er)
{
if (vars != null) vars(this, er);
Console.WriteLine("Thanks for your input and your values are validated");
}
public override int Add(Object ob)
{
int fr = base.Add(ob);
OnChanged(EventArgs.Empty);
Console.WriteLine("Your input is added and checking with our backend validationonce completed will get back to you.");
return fr;
}
public override void Clear()
{
base.Clear();
OnChanged(EventArgs.Empty);
Console.WriteLine("Thanks for your second Example your input is validated and cleaned by the browser end");
}
public override object this[int ind]
{
set
{
base[ind] = value;
OnChanged(EventArgs.Empty);
}
}
static void Main(string[] args)
{
first fr = new first();
Console.WriteLine(fr.Add(736));
Console.WriteLine("Thanks for your second Example");
}
}
Output:
In the second example, we used some additional default methods like OnChanged(), Add(), Clear(); these are the pre-defined methods for cleaning the garbage collections whenever the object is created and allocate the memory if we want to remove the unwanted reference by using the default methods clear(), remove() we can remove it.
Example #3
using System;
public class examp1
{
public event EventHandler evnts
{
add
{
Console.WriteLine("Welcome To My DOmain Your third Example Started");
}
remove
{
Console.WriteLine("Please provide your inputs");
}
}
}
public class examp
{
public void demo()
{
examp1 emp = new examp1();
emp.evnts += third;
emp.evnts -= third;
}
public void third(object snd, EventArgs er)
{
}
public static void Main(string[] args)
{
examp examp = new examp();
examp.demo();
Console.ReadKey();
Console.WriteLine("Your examples are started and executed successfully");
}
}
Output:
In the final example, we used the event handlers class and its default keywords like add and remove for adding and removing the values from memory. These values are calculated and called by each class instance and their methods. If the method has the parameters, it will be calling and passing it with the help of their instance.
Rules and Regulations for object sender
- The object sender must contain the EventArgs with instances; then, only the object sender will be performed with their user activities.
- In Big enterprise applications, the object creation and allocation will take more time, and also sending their reference also takes longer.
- By using synchronization, it avoids the deadlock in the process threads.
Conclusion
C# has so many predefined keywords, methods, and their attributes for each set of elements it has its own syntax and rules for utilizing the code in a more sophisticated and avoids duplicates, code redundancy. Like that object, the sender is also one of the parameters which are passing it to the methods for their class reference and usages.
Recommended Articles
This is a guide to C# object sender. Here we discuss How does object sender work in C# and Examples along with the outputs. You may also have a look at the following articles to learn more –