This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# 2.0 generics: How to create an Action object with zero parameters
I use code to delegate method like this:
this.Invoke((Action)(() => importProcessExited()));
And I'm getting an error:
Using the generic type 'System.Action' requires 1 type arguments.
How this line of code supposed to be in .NET 2?
You can simply create a new delegate without type arguments.
delegate void Action();
Related
This question already has answers here:
How do I use reflection to call a generic method?
(8 answers)
Closed 2 years ago.
Why can't I do this?
services.AddSingleton<Type.GetType("ShoppingCartCache",true)>();
OR is there a better way of passing from a string
The method also has an overload to pass a type as parameter. So you can do:
services.AddSingleton(typeof(ShoppingCartCache));
Same also works with your example (although more prone to runtime errors):
services.AddSingleton(Type.GetType("ShoppingCartCache",true));
The reason it doesn't work is because generic types must be static, thus known at compilation.
This question already has answers here:
Why use ThreadStart?
(2 answers)
Threads and delegates — I don't fully understand their relations
(2 answers)
Closed 6 years ago.
Im confused. The docs say a ThreadStart or ParametrizedThreadStart delegate is expected, but a method reference can be passed. How come it accepts a simple reference to a method, why is there no constructor signature that specifically determines this? Or is a delegate functionally identical to a method reference when it comes to type checking?
Im kind of inexperienced when it comes to .net, and this is very confusing to me.
Thanks in advance.
A delegate is a "method reference" of sorts, and by definition a reference to a method is too. So the two are equivalent:
var thread = new Thread(new ThreadStart(MyMethod));
// or
var thread = new Thread(MyMethod); // Assuming Mymethod conforms to the right spec
This question already has answers here:
C#: Restricting Types in method parameters (not generic parameters)
(6 answers)
Closed 7 years ago.
I have a method that accepts a Type as a parameter:
public void DoThings(Type x){...}
How can I restrict the passed Type to a known interface?
Use generics instead:
public void DoThings<X>() where X: IKnown { ... }
Otherwise, if you're forced to use a Type instance, you'll have to do a runtime check and throw an exception if x isn't what is expected.
This question already has answers here:
Generating Delegate Types dynamically in C#
(2 answers)
Closed 8 years ago.
In C# can you do something like
Func<typeof(variableType),int)> myDelegate;
where you can pass the type arguments dynamically to a delegate?
No. typeof() is evaluated at run-time. Your delegate declaration is evaluated at compile time. The typeof() evaluation would have to occur first for this to work.
You can not use Func<typeof(variableType),int)> myDelegate;.and get syntax error.
Use:
Func<object,int> myDelegate;
or:
Func<dynamic,int> myDelegate;
And see this:Generating Delegate Types dynamically in C#
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can you use optional parameters in C#?
I found in a project where I'm working[language: c#]. A strange signature(at the least for me) like that:
AccessModifier NameOfMethod(sometype param1, bool prmFlagOrSomething = false)
In the msdn library doesn't exist any reference to this kind of method.
That allow avoid pass the parameter prmFlagOrSomething, in this case prmFlagOrSomething have the value false.
That's what happened, but exist documentation?
What's the real name of this kind of method, or parameter?
That's a default parameter. Or as MS calls it, an "Optional Argument":
http://msdn.microsoft.com/en-us/library/dd264739.aspx
It's just an Optional Argument