Interesting Idea on Using a Func<string>






4.08/5 (11 votes)
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:
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:
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:
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:
var message = Lookup[i]
However, this does not do the same thing. To do the same thing would be the following:
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