An attempt to implement Trait (which is already made in PHP) in C#

Rajnilari2015
Posted by in C# category on for Beginner level | Points: 250 | Views : 2155 red flag
Rating: 3.5 out of 5  
 2 vote(s)

In this small tutorial, we all will see how to make a Trait (which is a concept of object-oriented programming) in C#. At this juncture, I must admit that, it is my way of implementation. There may be other way of doing so. Readers are always welcome to put their thoughts.


 Download source code for An attempt to implement Trait (which is already made in PHP) in C#

Introduction

In PHP 5.4.0, they have introduced a concept call as Traits that basically helps to reuse the code.But basically, Trait is a general object-oriented programming concept which can be implemented in any OOPS based language. In this article, we will look into implementing the same in C#

So what is Traits?

As per the PHP Manual, it says

A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own. It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance.
An ideal usecase for a Trait implementation should be creating traits that fulfill the capabilities required by an interface.

Let see an example(in PHP)

	interface ISomeInterface
	{
	    public function FunctionOfInterface();
	    public function ACommonFunction();
	}

	trait ExampleTrait
	{
	    public function ACommonFunction()
	    {
	        //function implementaiton
	    }

	    private function FunctionOfTrait()
	    {
	        //function implementaiton
	    }
	}

	class A implements ISomeInterface
	{
	    use ExampleTrait;

	    public function FunctionOfInterface()
	    {
	        echo 'Call from Class A for the Interface';
	    }
	}

	class B implements ISomeInterface
	{
	    use ExampleTrait;

	    public function FunctionOfInterface()
	    {
	        echo 'Call from Class B for the Interface';
	    }
	}
 

Let us observe the above example. The ACommonFunction is a method/function that is common to both the interface and Trait. Howevever, the function implementation is being done in Traits. And when Class A and B has implemented the Interface as well as the traits, then the concrete classes are rather implementing that interface function which has not been impleneted in the Trait.(which is FunctionOfInterface here).

In other words,trait is a completely isolated entity that has no dependencies on concrete classes.

Can we have the same concept in C#?

An attempt to made such an example is already shown here. But the OP of SO said

....It isn't quite as nice as I'd like, but it's not too bad either.

Henceforth, I have made an attempt to do the same.Let's see the code first

	namespace TraitExample
	{
	    interface ISomeInterface
	    {
	        void FunctionOfInterface();
	        void ACommonFunction();
	    }

	    public abstract class Trait
	    {
	        public Trait()
	        {
	            ACommonFunction();
	            FunctionOfTrait();
	        }

	        //Note~ it is public and not private
	        public void ACommonFunction()
	        {
	            System.Console.WriteLine("Common Function - implemented in trait");
	        }

	        private void FunctionOfTrait()
	        {
	            System.Console.WriteLine("Trait specific function");
	        }
	    }

	    public class A : Trait, ISomeInterface
	    {
	        public A() : base() { }       

	        public void FunctionOfInterface()
	        {
	            System.Console.WriteLine("Call from Class A for the Interface");
	        }
	    }

	    public class B : Trait, ISomeInterface
	    {
	        public B() : base() { }

	        public void FunctionOfInterface()
	        {
	            System.Console.WriteLine("Call from Class B for the Interface");
	        }
	    }
	}

    //And here is my invocation

    using System;

	namespace TraitExample
	{
	    class Program
	    {
	        static void Main(string[] args)
	        {
	            A objA = new A();
	            objA.FunctionOfInterface();
	            Console.WriteLine("\n--------------------------------------------------------\n");

	            B objB = new B();
	            objB.FunctionOfInterface();

	            Console.ReadKey();
	            
	        }
	    }
	}
 

The output is

What the code says

Let us first get into the statement about Trait from PHP Manual

a)A Trait is similar to a class

b)It is not possible to instantiate a Trait on its own

In C# language paradigm, it is the ABSTRACT CLASS that does the trick

And with the help of Interface and Abstract class (which acts as a Mixin here), we can arrive at the Horizontal Composition (which is the application of class members without requiring inheritance.)

That means

 class A extends Trait implements ISomeInterface
{
} // end of class A
 

N.B.~ I have implemented the concept of Trait in the C# way ,in the way I interpreted it.If anyone feel that my understanding is wrong, they can come out with a proper feedback.Also readers are encouraged to come out with their ideas for the same purpose.

Zip file attached.

References

a) PHP: Traits vs. Interfaces

b) Trait (computer programming)

Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)