Extension Method Implementation C# with Example



Extension Method Implementation C# with Example

 using System;

namespace CSharpInheritance
{
	class MyData
	{
		private double D1; // Fields
		private double D2;
		private double D3;

		public MyData(double d1, double d2, double d3) 	// Constructor
		{
			D1 = d1; D2 = d2; D3 = d3;
		}

		public double Sum() 				// Method Sum
		{
			return D1 + D2 + D3;
		}
	}
	
	static class ExtendMyData
	{
		public static double Average(this MyData md) 
		{
			return md.Sum() / 3;
		}
	}

	class UsingExtendMyData
	{
		public static void Main() 
		{
			MyData md = new MyData(3, 4, 5);
			Console.WriteLine("Average: {0}", ExtendMyData.Average(md));
			Console.WriteLine("Average: {0}", md.Average());
		}
	}
} 

0 Comment's

Comment Form

Submit Comment