C# FAQs

What is the benefits of using C# Properties over Fields?
Many benefits-
1. Properties are used for data binding; Fields aren’t.
2. Encapsulation can be achieved with Properties, not with Fields. An example below-

public int Order
{
    get { return m_order; }
    set
    {
        //Encapsulation goes here
        // Put some rules checking here. Maybe a call to make sure that the order
        //isn't duplicated or some other error based on your business rules.
        m_order = value;
    }
}

3. Set part of Properties can raise notification events such as INotifyPropertyChanged.PropertyChanged which other objects can listen for and update the display value. This is important part of advanced data binding.

In short, its a new “Standard” of data storage that has advanced facilities than just mere storing the data in the members of class. By avoiding Properties typically you can perform all functions, but implementation may differ from person to person. Using Properties is a standard which helps everyone to define/ access/ validate/ notify the data storage in one single form.

What is the benefits of having/ using Interfaces?
Consider the following code snippets-

// Code for the IAccount interface module.
 public interface IAccount
 {
   void PostInterest();
   void DeductFees(IFeeSchedule feeSchedule);
 }
 class BusinessAccount : IAccount
 {
   void IAccount.PostInterest()
   {
   // Code to post interest using the most favorable rate.
   }

   void IAccount.DeductFees(IFeeSchedule feeSchedule)
   {
   // Code to change a preferred rate for various services.
   }
 }

A very general question is- Why not just have the class BusinessAccount and two functions in it PostInterest() and DeductFees()?

Answer is: using Interfaces in C#, we can create types that support multiple behaviors. In the above example, if you did not implement the interface, if you had another class with methods PostInterest and DeductFees, you would have to check for type before converting them to their type and then calling their methods. With the Interface, you only need to cast it to the interface (if it is not passed as the interface type) before calling the methods.
Interfaces allow to create nice layouts for what a class is going to implement. When many components use the same interface it allows us to easily interchange one component to another which is using the same interface. Dynamic programs can be easily created with this feature.

Is it possible to create an Interface that derives multiple Interfaces?
Yes, in C# it is permissible to create an interface that derives multiple base interfaces. Example-
public interface ICar
{
void Drive();
}
public interface IUnderWaterCar
{
void Drive();
}
//implementing an interface with two interfaces
public interface ITarjanWonderCar : ICar, IUnderWaterCar
{
void TurboDrive();
}

Please be aware that it is not permissible to build a class that derives from multiple base classes.

What is Method Overloading and overriding?
Overloading
Overloading is when you have multiple methods in the same scope, with the same name but different signatures-

//Overloading
public class test
{
    public void getStuff(int id)
    {}
    public void getStuff(string name)
    {}
} 

Overriding
Overriding is a principle that allows you to change the functionality of a method in a child class.

//Overriding
public class test
{
        public virtual getStuff(int id)
        {
            //Get stuff default location
        }
}
public class test2 : test
{
        public override getStuff(int id)
        {
            //base.getStuff(id);
            //or - Get stuff new location
        }
} 

4 Responses to C# FAQs

  1. Anonymous says:

    Respect to the post author. This really is genuinely some relevant information and facts.

  2. I’ve read your write-up & chavih I’m posting here just to let you know that you are doing a terrific job by keeping us posted about this. Please maintain on posting such quality articles as this is a rare factor to get these days

  3. I am definitely impressed together with your writing skills as well as with the layout on your blog.

  4. It is really a nice and useful piece of information. I am happy that you simply shared this helpful info with us. Please stay us up to date like this. Thank you for sharing.

Leave a Reply