Solved

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

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a Dropdown list with sub group 10 39
ASP.NET Built-In Report Creator / Viewer 5 40
Put File 6 34
How do I "share" on social sites? 2 36
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

737 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