c#


Action returning void and taking parameter, with ternary operator


I want to write an Action which only takes a PerformanceCounterCategory as a parameter. I know there is Action<>, Func<> and Delegates and there is some distinction between these, but I am not sure what it is. Can someone please tell me what the difference is (I read somewhere that Action does not return, or this may be Func).
I am trying to write something like the following:
Action<PerformanceCounterCategory> action = (int > 5) ? action1 : action2;
action1 and action2 are both methods which return void but take PerformanceCounterCategory as the (only) parameter.
Is this the right way to go? I keep getting errors about method group/void etc so I am not confident the code above is the best for my needs.
Thanks
You'll need to cast one side or other - or not use the conditional operator.
Basically, ignore the assignment - because the compiler does. It doesn't use the fact that you're trying to assign to a variable to work out the type of the conditional expression. We're left with just:
(i > 5) ? action1 : action2
as the expression. What's the type of that? What delegate type should the method groups be converted to? The compiler has no way of knowing. If you cast one of the operands, the compiler can check that the other can be converted though:
(i > 5) ? (Action<PerformanceCounterCategory>) action1 : action2
Alternatively:
Action<PerformanceCounterCategory> action = action2;
if (i > 5)
{
action = action1;
}
It's unfortunate, but that's life I'm afraid :(
To start by answering your questions about the differences between Action and Func
An Action has a void return.
A Func returns a value. You specify the type of the return value as the first generic parameter
Func<ReturnType, ParameterType1, ParameterType2...etc>
There are two problems with your code snippet.
(int > 5) doesn't work. int is a data type. You need to create a variable and check it's value.
int myValue = 4;
... (myValue > 5) ? ... etc
when using the conditional operator (? :) both options must be of the same type. If action1 and action2 are different methods they aren't of the same type, (even if they match the same delegate). The compiler won't be able to figure out which should be converted to which. What you would need to do is cast one to a delegate that matches the other so the compiler can figure out how to convert the two to matching types.
Like this:
Action<PerformanceCounterCategory> action = (int > 5) ? (Action<PerformanceCounterCategory>)action1 : action2;

Related Links

LINQ joining values from different classes
wp7 xna gestures not working
New window replicates
ASP.net C# Return JS Alert from another thread
Asp.net C# Webform forms Generator/Builder [closed]
Lambda event handler scope
Get OS-Version in WinRT Metro App C#
Enumerating DataPoints in view
Switching control backColor fast
How can a view fill a model?
Iterating form controls that are created at runtime
How to use a Resource Image in Word document?
detecting type of generics within generics
Stuck with httpwebrequest usage
ScriptManager Error in my code?
How to reduce the number of TCP ACK's during a highly reliable bulk transfer

Categories

HOME
reactjs
drupal
logging
ckan
stata
virtual-machine
xamarin.forms
c++builder
coordinates
download
aggregation-framework
comments
servicenow
qt4
sitemap
webpack-dev-server
cracking
rename
elastalert
osclass
distinct
http-status-codes
javascript-debugger
aws-devicefarm
ajaxcontroltoolkit
ng-tags-input
sasl
android-6.0-marshmallow
formtastic
pypy
kundera
xlib
tikz
visual-studio-debugging
wercker
azure-vm-scale-set
extern
stochastic-process
iterm2
aspose.pdf
icu
mixed-models
paperclip
text-extraction
f#-fake
scrapinghub
text-formatting
geminabox
g77
stanford-nlp-server
cloudconvert
extjs3
riemann
htop
android-vectordrawable
jtextarea
printer-control-language
codesniffer
huawei
spring-social-twitter
x12
vga
syslog4j
diameter-protocol
pyshark
jsondoc
wordpress-plugin
apache2-module
fadein
markerspiderfier
imageicon
play-json
musicbrainz
uinavigationitem
roslyn-code-analysis
syncano
polarion
photogrammetry
register-allocation
go-gin
selenium-grid2
im4java
smartxls
elixir-framework
mysql-error-1146
xcode6.1-gm-seed
code-complete
google-oauth-java-client
aspnet-compiler
nokogiri
backbone-views
asp.net-4.0
reference-manual
rfc822
orchestration
derived-class
waveout
designated-initializer
browser-state
newspeak
pixel-bender
retrospectiva

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App