?
Solved

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

Posted on 2010-09-15
5
Medium Priority
?
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 12

Accepted Solution

by:
w00te earned 600 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 600 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 800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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