C# Do not define variable for not used out parameter [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a way to omit out parameter?
Instead of:
SomeType param3;
SomeMethodCall(param1, param2, out param3);
I want not to define param3 in the case I don't need its value.
Is there any way to achieve this?

No, there is no way of doing that in C#.

If SomeMethodCall is something you defined yourself then you can overload the method. If it's not then you can't.

No, you'll have to actually create a variable of the correct type if you want to use a function that takes an out parameter.

Related

Is there a way to shorten field declaration in C#? [duplicate]

This question already has answers here:
Is there a way to abbreviate a custom class type declaration?
(4 answers)
Implicit typing; why just local variables?
(6 answers)
Closed 2 years ago.
Why do I have to write:
public VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName> field = new VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>();
Instead of:
public var field = new VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>();
Is there any way to shorten this ridiculous declaration?
Why do I have to write two identical types in a single line?
Upd.
I have found that using "dynamic" keyword instead of "var" perfectly solves this problem!
Please feel free to provide any info on the perfomance (or other) issues with this solution!
You can use a type alias. Documentation can be found here. Link to alias for generic class: here
using YourShortName = VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>;
Usage
public YourShortName field = new YourShortName();
See here for why you can only use var for local variables.
And see here for an in depth look at dynamic and why it's not even remotely close to using var.

how to access one class property in another class linq [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
how to make an variable accessible to other class(within same csproj)
I have a variable
int principleIndex = Principles.Instance.RowIndexToPrincipleIndex(hti.Row);
I want to use it in another class linq query:
principlesList.Select(p => p.GetInstanceForDatabase()).where(p=>p.principleIndex ).ToList()
but principleIndex is not accessible here.
How do I do it??? I tried to make it static and I also tried to use it inside a property, but does not work.
Is principleIndex defined in the same scope as your second line of code? If so perhaps your Where call should read something like .Where(forDBInstance => forDBInstance.Index == principleIndex)

Default for generic type? [duplicate]

This question already has answers here:
Is there a reasonable approach to "default" type parameters in C# Generics?
(6 answers)
Closed 9 years ago.
Is it possible to do something like
public class PriorityQueue<TValue, TPriority=int> where TPriority : IComparable
(note the =int) ?
Before you suggest it, yes, I know I can just add another line:
public class PriorityQueue<TValue> : PriorityQueue<TValue, int> { }
But I'm wondering if it's possible to do it as a param.
No. There is no option for default types on generic types in C#.
Your second example is often the "best" option available, if you need this behavior.

How to find out all possible values of an enum? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I enumerate an enum?
Say I have an enum type MyEnum. Is there a way in C# to get a list of all possible values for an enum of type MyEnum?
Enum.GetValues
An instance of the enum can have any assignable to the underlying type (i.e., int.MinValue through int.MaxValue for any regular enum). You can get a list of the named values by calling Enum.GetNames and Enum.GetValues.
Enum.GetValues(typeof(SomeEnum));
will return an array with all the values. I do not know if this helps you.

Getting the name of the parameter passed into a method [duplicate]

This question already has answers here:
How can I get the name of a variable passed into a function?
(23 answers)
Closed 9 years ago.
Duplicate:
Determine the name of the variable used as a parameter to a method
Is there any way to retrieve the name of a parameter that was passed into a method e.g.
int someParameter = 1;
Method(someParameter);
public void Method(int parameter)
{
// I want the name of 'parameter' which will be 'someParameter'.
}
No. It's only the value which is passed in as the argument. All the method gets is the integer. The fact that the expression happened to be just evaluating a variable is unknown as far as your method is concerned.
No, there is no way.
This will be followed by a bunch of people showing weird lambda expression ways to change the call site and kinda get the name, but the short answer is no.
The only way perhaps will be with annotations with run-time retention. But then, it will be the name of the annotation, not of the parameter itself. A parameter name is just a syntactic artifact of the language. It does not get carried over to the compiled result.

Categories

Resources