What's the name of this kind of method, parameters? [duplicate] - 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

Related

Passing <T> from a string parameter to use AddSingleton [duplicate]

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.

Why can i pass a function name to the System.Threading.Thread constructor [duplicate]

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

Getting a type representation of a partially open generic type [duplicate]

This question already has answers here:
C# Language: How to get type of bound but open Generic class?
(4 answers)
Closed 6 years ago.
I am wondering if anyone knows a way to get a representation of a partially open generic type in C#, for example IDictionary<string,>.
What I have tried:
typeof(IDictionary<string,>)
I get: Partially opened type is not permitted in 'typeof' expression compile error.
typeof(IDictionary<,>).MakeGenericType(typeof(string))
I get: ArgumentException (The number of generic arguments provided doesn't equal the arity of the generic type definition. Parameter name: instantiation)
Why I want to do this:
I have a number of scenarios where I need to check if a class I have implements an interface, but in several cases I know that some of the generic type parameters have to be specific (i.e. I want something that implements a dictionary with string keys but I don't care about the value type). I realize there are a number of other ways I could do this (for example, by providing an array of necessary generic parameter types to my method). But in looking at the problem I got curious if there is a way to specify partially open / partially closed generic types, hence the question.
Maybe something like that could answer to your problem:
class CustomDictionary<TValue> : Dictionary<string, TValue>
{
}
And this should work:
typeof(CustomDictionary<>).MakeGenericType(typeof(string))

Difference between ContentPropertyAttribute and ContentProperty [duplicate]

This question already has answers here:
What's the difference between [Something] and [SomethingAttribute] [duplicate]
(3 answers)
Closed 7 years ago.
I hope this wasn't asked already. But i found nothing. If something exists, thanks for the note.
The title says it all i think.
I've seen these two variants. But in my opinion it does the same. And why can i use both. Thanks for education.
// variant 1
[ContentProperty("Text")]
// variant 2
[ContentPropertyAttribute("Text")]
You can omit the word "Attribute" when writing attributes over something. The actual class is called ContentPropertyAttribute. Both of your lines do exactly the same and use the exact same attribute class.

Why does C# make a distinction between ref and out? [duplicate]

This question already has answers here:
What's the difference between the 'ref' and 'out' keywords?
(28 answers)
Why ref and out in C#?
(7 answers)
Closed 9 years ago.
As per this post, the reason there is a distinction between ref and out is because it is costly to copy the value of the variable when using ref.
Why is there a need to marshall in the first place? Doesn't C# just pass the pointer under the hood? In that case, there would be no need to copy values.
Because the semantics of the two are completely different.
An out parameter is used to indicate that it will be used to return (output) a value, nothing more.
A ref parameter on the other hand indicates that an existing object (variable) should be passed to the method by reference. In the context of C#, an object passed by reference (not to be confused by reference types) is often a hint that the method will (and should) modify that object. It shouldn't be used "just because." It is generally used only for value types since it is the only way to get reference semantics for them.

Categories