Passing void as out parameter [duplicate] - c#

This question already has answers here:
How to explicitly discard an out argument?
(8 answers)
Closed 6 years ago.
Is there a way to ignore an out parameter? I.e.
bool myIntIsValid = int.TryParse(stringValue, void);
or
bool myIntIsValid = int.TryParse(stringValue, out new int());

No, you have to pass it a variable, but you're free to ignore it afterwards.

Related

C# Check if a method exists in TDD [duplicate]

This question already has answers here:
How to check whether an object has certain method/property?
(5 answers)
Reflection GetMethod. select a more specific method
(3 answers)
Closed 11 months ago.
In Java I can check if a method named "addNumbers" exists in a class "Calculate" by doing this:
Class<?> c = Class.forName("com.example.package.Calculate");
Method m = c.getMethod("addNumbers", int.class, int.class);
m.invoke(5,7)
What is the C# equivalent for this example?

assigning type to a variable and using it to initialize other objects [duplicate]

This question already has answers here:
Declare a variable using a Type variable
(6 answers)
Closed 2 years ago.
Is it possible to do something like this:
tybeVariable = Double;
typeVariable newDoubleVariable = 5;
I want to define a variable which will contain a type itself and then to initialize objects with it.
You can use var keyword. Its implicitly type keyword. You can read more about that here

How to declare an Optional type in C#? [duplicate]

This question already has answers here:
Optional return in C#.Net
(13 answers)
Closed 8 years ago.
Java 8 has Optional<T> which is nice way to declare optional types as described here.
Is there an equivalent way to that in C# ?
As per this answer You could make a struct that mimics this this type of functionality.

Is that correct using of "this" keyword? [duplicate]

This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
In C#, is "this" keyword required? [duplicate]
(6 answers)
Closed 9 years ago.
I would like to know if I get it correctly: also with this keyword I can distinct between fields and variables?
Like this:
class X
{
int x;
public X(int x)
{
this.x=x;
}
}
Yes, if a method parameter (or local variable) has the same name as a field, you need to use this to distinguish the two. Also, StyleCop is very vocal about every class member access being done through this, but whether that's a good idea or not may be up to debate. It makes things more clear, but also adds much visual clutter.

var in C# - Why can't it be used as a member variable? [duplicate]

This question already has answers here:
Implicit typing; why just local variables?
(6 answers)
Closed 8 years ago.
Why is it not possible to have implicitly-typed variables at a class level within C# for when these variables are immediately assigned?
ie:
public class TheClass
{
private var aList = new List<string>();
}
Is it just something that hasn't been implemented or is there a conceptual/technical reason for why it hasn't been done?
Here's a blog post from Eric that explains the reasoning.

Categories