Solved

What is the purpose of defining a <Func> delegate as a property?

Posted on 2010-09-15
5
394 Views
Last Modified: 2013-11-07
hi,

I have the below line in my code which was written by somebody else. What is the purpose of defining like this and how to use this? Thank you.

public Func<string, string> GetValue { get; set; }
0
Comment
Question by:ipjyo
5 Comments
 
LVL 12

Accepted Solution

by:
w00te earned 150 total points
ID: 33686608
Well assuming it is a delegate, you may want to do that to allow the programmer using the class this appears in to change the function the delegate calls.  For example, if you're doing a math unit and want to be able to pass in an opeartion to a function say:

int yourfunc(delegate)
{
     int x = 7;
     int y = 9;
     return delegate (x, y);
}

the delegate could point to any int,int function returning an integer, so you could pass in a divide, multiply, square, log, whatever function.  So, your delegate probably can be chaned to configure the operation of the functions using it.

-w00te
0
 
LVL 12

Expert Comment

by:w00te
ID: 33686622
PS - this is a simple way to implement a simple strategy pattern, though much of the time people implement this using some beefy OO inhertiance instead.

-w00te
0
 
LVL 8

Assisted Solution

by:Gururaj Badam
Gururaj Badam earned 150 total points
ID: 33689019
Func or in your case Func is a delegate which takes string as input and returns string.

I can't figure out the strategy unless you reveal more details on how it's being used in your code. But in generally the intent could be that your code wishes to expose a method so that the caller can initiate an action within the contain class.

It will be nice if you can tell us how it's being used out the class to help explain it better.
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 200 total points
ID: 33696967
These guys are both right, I'm going to try to clarify slightly (although I may only succeed in further muddying the issue)...
I write a class with such a property, and provide it to you in a DLL.  This class has three properties, two int's (NumberOne and NumberTwo), and the third property is a Func<int, int, int>. The class has some method, let's say it's called "PerformOperation".
You write a method in your code called "Add" that takes two int's as parameters and returns an int.  You assign this method to the Func<int,int,int> property of my class. Now, in my class when PerformOperation is called I can call whatever method was assigned to the Func<...> property - which, in this case, is your Add method.  In this manner, you could change the behavior of "PerformOperation" by changing the method pointed to by the Func<...> property - it's a means to afford you, the consumer of the class, some flexability.
Here's a simple example how you can change the behavior of the OperationResult property by changing the method pointed to by the OperationMethod property - in the first case "OperationResult" is the addition of two numbers, and in the second the same property, "OperationResult", is the multiplication of the two numbers.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace ConsoleApplication1

{

	class Program

	{

		private static readonly string[] Bands = { "one", "Two", "three" };

		static void Main(string[] args)

		{

			MyClass testClass = new MyClass();



			testClass.NumberOne = 3;

			testClass.NumberTwo = 9;



			// Assign the "Add" method to the Func<int, int, int> property

			testClass.OperationMethod = Add;

			// Now the OperationResult property will return 12

			Console.WriteLine(testClass.OperationResult.ToString());



			// Assign the "Multiply" method to the Func<int, int, int> property

			testClass.OperationMethod = Multiply;

			//Now the OperationResult property will return 27

			Console.WriteLine(testClass.OperationResult.ToString());



			Console.ReadKey();

		}



		private static int Add(int a, int b)

		{

			return a + b;

		}



		private static int Multiply(int a, int b)

		{

			return a * b;

		}

	}



	public class MyClass

	{

		public int NumberOne { get; set; }

		public int NumberTwo { get; set; }

		public Func<int, int, int> OperationMethod { get; set; }



		public int OperationResult

		{

			get { return OperationMethod(this.NumberOne, this.NumberTwo); }

		}

	}

}

Open in new window

0
 

Author Comment

by:ipjyo
ID: 33697137
Thanks for the suggestions everybody. I will test it the way you suggested to understand better and also let you know more details of my code. Thank you.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now