Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / C#
Tip/Trick

Interesting Idea on Using a Func<string>

Rate me:
Please Sign up or sign in to vote.
4.08/5 (11 votes)
27 Apr 2016CPOL2 min read 14.3K   6   8
I have used a Func<string> to set the value of a property.

Details

I was just wondering if I could use a switch statement to set the value of a message. It seemed to me that should be able to just use a lambda expression, but that did not work, and this is what finally worked:

C#
Message = (new Func<string>(() =>
{
   switch (e.HolosState)
   {
      case HolosState.CoolDown: return "Holos busy with clean up from previous session";
      case HolosState.Capturing: return "Holos is running";
      default: return string.Empty;
 }})).Invoke();

Not sure about the practicality, or if it is an improvement on readability, but think it is an interesting idea. Using the C# conditional operator (?:) is an option that is also terse, but think its readability suffers.

Thoughts

This was really more of an exercise to see how it could be done, and not something that I think is really good programming. There is a lot of extra characters that clutter up everything. What I think might have been good if the language supported it is something like this:

C#
Message = () =>
{
   switch (e.HolosState)
   {
      case HolosState.CoolDown: return "Holos busy with clean up from previous session";
      case HolosState.Capturing: return "Holos is running";
      default: return string.Empty;
 }};

Or maybe even better:

C#
Message =>
   switch (e.HolosState)
   {
      case HolosState.CoolDown: return "Holos busy with clean up from previous session";
      case HolosState.Capturing: return "Holos is running";
      default: return string.Empty;
   };

Other Suggestions

There have been suggestions on how to accomplish the same thing that I did above, but they are worse than the solution I proposed in some ways. There was:

C#
var message = Lookup[i]

However, this does not do the same thing. To do the same thing would be the following:

C#
var message = (new Dictionary<int, string>
{
 {1, "Holos busy with clean up from previous session"},
 {2, "Holos is running"}
})[caseIf];

All of a sudden, this does not seem so good. To handle all the default cases, which could be so many, a line would be needed for each default cases needed, and it seems that creating a dictionary to do this would create more overhead, but I cannot be sure.

Basically, the method suggestion really just is encapsulating what has already basically been encapsulating, and I feel would obsolete the code.

The suggestion to use reflection only makes any sense if the only purpose of the enumeration is for this message, and in the case I was using, it was more broadly used. Maybe if I wanted a message like $"The current state of the system is {e.HolosState}" it might make sense, but then I would not have needed to do anything like this.

Conclusion

In any case, there are many that misunderstood the purpose of this tip. It was really more of a presentation on how to possibly use the Func capability, and not one where I was suggesting to anyone else that this is the best way to solve what is really a trivial problem.

History

  • 04/27/2016: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at qck1@hotmail.com.

Comments and Discussions

 
QuestionDescription Attribute Pin
Sinisa Hajnal28-Apr-16 20:17
professionalSinisa Hajnal28-Apr-16 20:17 
SuggestionAttributes can accomplish this Pin
Chris Copeland27-Apr-16 22:38
mveChris Copeland27-Apr-16 22:38 
AnswerRe: Attributes can accomplish this Pin
Clifford Nelson28-Apr-16 5:28
Clifford Nelson28-Apr-16 5:28 
QuestionYou should create a method getMessage, and Message= getMessage(); Pin
LoveJenny27-Apr-16 21:53
LoveJenny27-Apr-16 21:53 
GeneralI think it's a misuse of Func and lambdas Pin
Klaus Luedenscheidt27-Apr-16 18:54
Klaus Luedenscheidt27-Apr-16 18:54 
AnswerRe: I think it's a misuse of Func and lambdas Pin
Clifford Nelson28-Apr-16 5:26
Clifford Nelson28-Apr-16 5:26 
QuestionThat seems overly complicated... Pin
Marc Clifton27-Apr-16 14:21
mvaMarc Clifton27-Apr-16 14:21 
AnswerRe: That seems overly complicated... Pin
Clifford Nelson27-Apr-16 15:07
Clifford Nelson27-Apr-16 15:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.