Solved

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

Posted on 2010-09-15
5
404 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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