Friday 20 September 2019

Default implementation of method in interface(New feature of interface)


The latest version of c# allows you to define body of interface method.



For example :-
Consider you have a project of Asset Management which has an interface IAsset and which has properties AssetName, AssetAge, Id and so on

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stucts.New_Features.Interface
{
    interface IAsset
    {
        string AssetName { get; set; }
        string AssetAge { get; set; }
        string Id { get; set; }

    }
}

Now consider you have this interface inherited in multiple classes like FixedAssets, TemporaryAssets and so on

using System;
using Stucts.New_Features.Interface;

namespace Stucts.New_Features
{
    class FixedAsset : IAsset
    {
        public string AssetName { get; set ; }
        public string AssetAge { get; set; }
        public string Id { get; set; }
    }
}

Consider you want to add new feature to you application which moves asset to archive after certain years as per customer needs. It may be possible that you need to implement this logic for all kind of asset or just for few or some different logic based on other calculation.
In older version you need to define a non body method in interface and implement that method in all the other classes and. In some cases this might not seem a very big issue but I know when you are working on an ultra large scale software then this is a headache no one wants.



In C# 8.0 you can define default implementation of a method in interface and classes can use that method or if you can give different definition to same method in child classes

        public void MoveAssetToArchive(int age)
        {
              //Your log here
        }

You can start using this method in all the child classes without implementing it

FixedAsset  _fixAsset = new FixedAsset () {
AssetAge="4",
       AssetName="HTC Explorer Mobile phone",
              Id = "1001"
       };
       IAsset _IAsset = _fixAsset;
       _IAsset.MoveAssetToArchive(4);

Notice the following section of the test:

IAsset _IAsset = _fixAsset;
       _IAsset.MoveAssetToArchive(4);


Well that saved lot of time and effort :)

Similarly you can also declare variable as well as static members in interface
I would love if you guys create a demo project with scenario where we might need to declare static members and behaviors in an interface. Please do try to create a Demo. I will create a demo and with a scenario and write it in the blog soon so that we can discuss it. Comment if you have any question or want to discuss something. Reach out to me at ishooagarwal@gmail.com. Please add [Blog] in subject so that I can differentiate regular mail.
Meanwhile follow and subscribe the blog to get notifications of the blog
Thanks.

No comments:

Post a Comment

Send All Azure App Insights Or Logs Data To Tool Like Datadog

  Introduction Microsoft Azure provides a variety of insights and monitoring logs that you can monitor to check the state of a resource and ...