Thursday, March 30, 2023

Async and await in C#

 Async and Await are the two keywords that help us to program asynchronously. 

An async keyword is a method that performs asynchronous tasks such as fetching data from a database, reading a file, etc, they can be marked as “async”.


The await keyword waits for the async method until it returns a value. So the main application thread stops there until it receives a return value.



Friday, March 24, 2023

What is difference between variable and properties in c#

1.  The crucial difference between them is that a variable represents a memory location but a property does not. A public read/write property, Prop of type T, corresponds to two methods:


2. Through property we can access the private member.


Properties are named members of classes, structures, and interfaces. Member variables or methods in a class or structures are called Fields. Properties are an extension of fields and are accessed using the same syntax. They use accessors through which the values of the private fields can be read, written or manipulated.

Properties do not name the storage locations. Instead, they have accessors that read, write, or compute their values.

For example, let us have a class named Student, with private fields for age, name and code. We cannot directly access these fields from outside the class scope, but we can have properties for accessing these private fields.


Example:

The following example demonstrates use of properties:


using System;

class Student

{


   private string code = "N.A";

   private string name = "not known";

   private int age = 0;


   // Declare a Code property of type string:

   public string Code

   {

      get

      {

         return code;

      }

      set

      {

         code = value;

      }

   }

   

   // Declare a Name property of type string:

   public string Name

   {

      get

      {

         return name;

      }

      set

      {

         name = value;

      }

   }


   // Declare a Age property of type int:

   public int Age

   {

      get

      {

         return age;

      }

      set

      {

         age = value;

      }

   }

   public override string ToString()

   {

      return "Code = " + Code +", Name = " + Name + ", Age = " + Age;

   }


   public static void Main()

   {

      // Create a new Student object:

      Student s = new Student();

            

      // Setting code, name and the age of the student

      s.Code = "001";

      s.Name = "Zara";

      s.Age = 9;

      Console.WriteLine("Student Info: {0}", s);

      //let us increase age

      s.Age += 1;

      Console.WriteLine("Student Info: {0}", s);

      Console.ReadKey();

    }

}

When the above code is compiled and executed, it produces following result:

Student Info: Code = 001, Name = Zara, Age = 9

Student Info: Code = 001, Name = Zara, Age = 10



Thursday, March 23, 2023

when to Use Abstract class and when to use Interface Real time scenrios.

 When we have the requirement of a class that contains some common properties or methods with some common properties whose implementation is different for different classes, in that situation, it's better to use Abstract Class then Interface.

Abstract classes provide you the flexibility to have certain concrete methods and some other methods that the derived classes should implement. On the other hand, if you use interfaces, you would need to implement all the methods in the class that extends the interface. An abstract class is a good choice if you have plans for future expansion.

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/