Wednesday 12 April 2023

Delegates in C#

 In C#, a delegate is a type that represents a reference to a method with a particular signature. It is a way to encapsulate a method reference so that it can be passed as an argument to another method, returned as a value from a method, or stored as a field or property.

To define a delegate, you need to specify the delegate's signature, which consists of the return type and the parameter types of the method it represents. For example, the following code defines a delegate type called "MyDelegate" that represents a method with a void return type and two integer parameters:

csharp
delegate void MyDelegate(int x, int y);

You can then create an instance of the delegate type and assign it a reference to a method that matches its signature. For example:

arduino
void MyMethod(int a, int b) { Console.WriteLine("MyMethod: {0}, {1}", a, b); } MyDelegate del = new MyDelegate(MyMethod); del(1, 2); // calls MyMethod with arguments 1 and 2

Delegates are often used in event handling, where a method is called when an event occurs. For example, the following code defines a class with an event called "MyEvent" and a method called "OnMyEvent":

csharp
class MyClass { public event MyDelegate MyEvent; public void RaiseEvent() { if (MyEvent != null) { MyEvent(1, 2); } } public void OnMyEvent(int x, int y) { Console.WriteLine("OnMyEvent: {0}, {1}", x, y); } }

You can then subscribe to the event by creating an instance of the class and adding a method reference to the event delegate:

scss
MyClass obj = new MyClass(); obj.MyEvent += new MyDelegate(obj.OnMyEvent); obj.RaiseEvent(); // calls OnMyEvent with arguments 1 and 2

In this example, when the "RaiseEvent" method is called, it checks if there are any subscribers to the "MyEvent" event and calls the "MyDelegate" delegate with the specified arguments for each subscriber. In this case, the "OnMyEvent" method is called with arguments 1 and 2

Monday 3 April 2023

Access modifiers in C#

 Access modifiers in C# are keywords used to control the accessibility of types and members in a class. There are five access modifiers available in C#:

  1. Public: A public member can be accessed from anywhere, inside or outside the class.

  2. Private: A private member can only be accessed within the class in which it is declared.

  3. Protected: A protected member can be accessed within the class in which it is declared, as well as by any derived class.

  4. Internal: An internal member can be accessed only within the same assembly (i.e., the same .exe or .dll file) in which it is declared.

  5. Protected internal: A protected internal member can be accessed within the same assembly in which it is declared, as well as by any derived class, whether it is in the same assembly or not.

To specify an access modifier for a type or member, the modifier keyword is placed before the type or member declaration. For example, to declare a public method, you would use the "public" keyword followed by the method signature:

csharp
public void MyMethod() { // method body }

It is important to choose the appropriate access modifier for each type and member to ensure that your code is secure and well-organized.


here are some examples of how access modifiers work in C#:

csharp
public class MyClass { public int publicField; private int privateField; protected int protectedField; internal int internalField; protected internal int protectedInternalField; public void MyMethod() { // code here can access all fields publicField = 1; privateField = 2; protectedField = 3; internalField = 4; protectedInternalField = 5; } } public class MyDerivedClass : MyClass { public void MyDerivedMethod() { // code here can access public and protected fields publicField = 1; protectedField = 2; // cannot access private or internal fields // privateField = 3; // error // internalField = 4; // error // can access protected internal fields protectedInternalField = 5; } } public class AnotherClass { public void AnotherMethod() { MyClass myClass = new MyClass(); // code here can access only public fields and methods myClass.publicField = 1; myClass.MyMethod(); // cannot access private, protected, internal or protected internal fields or methods // myClass.privateField = 2; // error // myClass.protectedField = 3; // error // myClass.internalField = 4; // error // myClass.protectedInternalField = 5; // error } }

In this example, we have a class called MyClass with five fields and a method. Each field has a different access modifier, and the method is public.

Then we have a derived class called MyDerivedClass that inherits from MyClass. In MyDerivedMethod, we can access public and protected fields, but not private or internal fields.

Finally, we have a class called AnotherClass that creates an instance of MyClass. In AnotherMethod, we can only access public fields and methods of MyClass. We cannot access any of the other fields or methods because they have a different access modifier.

Draw Circle in C#

 Here's an example program in C# that uses the `System.Drawing` namespace to draw a circle on a Windows Form: // Mohit Kumar Tyagi using...