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

IList to JSON fails
Convert to Nested JSON C#
Sending List<Object> WCF POST
Export to excel is exporting check box to excel instead of string
Loop iteration of an array string
Impersonate a user using the Session ID
Is it safe to store and update list in asp.net session via static methods?
Change of the edited cell in a datagridview cell
Trying a REST POST call in C# - using Postman code
List object initialization [duplicate]
How to access HttpContext.Current.User custom attributes in the view
Non-nullable property exception when inserting a new entity
Radio button limiting sets
C# MongoDB LINQ: Cannot query nested list
Fill only last 6 month data from current month in second list from first list in linq
Save an image to a specified path with default filename without SaveFileDialog in C#? [duplicate]

Categories

HOME
optimization
qemu
fortran
packet
window.open
assign
angularjs-directive
symfony-3.2
cdn
doctrine2
ephesoft
sahi
segmentation-fault
google-my-business-api
runtime
oms
captcha
camunda
android-notifications
openam
minimagick
turn.js
ajaxcontroltoolkit
devexpress-mvc
v4l2
scripting-language
meteor-accounts
git-submodules
azure-table-storage
concrete5
rtos
apache-falcon
machine-code
gitosis
mkdir
kofax
maven-2
extern
jvisualvm
google-cloud-powershell
autodesk-data-management
reqif
rivets.js
extjs3
enet
boost-regex
kendo-scheduler
grib
nsalert
trusted-computing
toggleclass
consolidation
hotkeys
jde
window.location
nd4j
oryx
wordpress-plugin
jersey-test-framework
apache2-module
overlapping
windows-phone-8-emulator
android-togglebutton
page-flipping
gcc-warning
httpmodule
douglas-peucker
grunt-contrib-connect
xmlserializer
redmine-plugins
javacompiler
rpt
debuggervisualizer
satisfiability
lettuce
elixir-framework
flask-mongoengine
ekevent
ardor3d
scidb
cab
nokogiri
azure-pack
mosix
latex-suite
windows-scheduler
magicsuggest
servicemanager
spawn
gpl
zepto
nsregularexpression
filedialog
flatten
erlide
reflexil
optimistic-concurrency
jmesa
helicontech
sessiontracking
noise-reduction
mass-emails
conditional-operator
dongle
mysql-pconnect
cons

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