[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2010-09-15
5
Medium Priority
?
420 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 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