This question already has answers here:
Is it possible to pass properties as "out" or "ref" parameters?
(3 answers)
Closed 8 years ago.
Why exactly I can't use an AutoProperty as an out parameter?
For example (This gives me an error):
public int HeightValue { get; set; }
//...
private void Parse()
{
int.TryParse(WidthText.Text, out HeightValue);
//Intellisense Error: out argument is not classified as a variable
}
Possibly because properties are in essence methods and you need to give a field to set the value to the out parameter. You can define a backing field for your property and give its value as the out parameter.
See Jon Skeet's answer here:
Passing a property as an 'out' parameter in C#
The method itself needs a variable as the out parameter. It's got to have a storage location it can just write values to. Not a property, not anything it needs to invoke: just a storage location. A property doesn't satisfy that requirement. So there's nothing that can be done by the compiler in the method to allow this.
Related
This question already has answers here:
string is reference type but why it work as a value type when it's assignment updated
(4 answers)
Closed 2 years ago.
I'm confused by the following. If string is a reference type in C# and gets passed as a reference type, why doesn't changing the parameter value inside the method lead to change of value in the original argument?
Surely, value at which the reference 'z' points to has been changed to "Mike" in the method?
public static void ChangeStudentName(string param)
{
param = "Mike";
}
string z = "Bill";
ChangeStudentName(z);
Console.WriteLine(z);
Output - Bill
You want ChangeStudentName(ref string param). Please see the explanation ref (C# Reference). From the article:
Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same. A method parameter can be modified by ref regardless of whether it is a value type or a reference type. There is no boxing of a value type when it is passed by reference.
This question already has answers here:
Impossible to use ref and out for first ("this") parameter in Extension methods?
(6 answers)
Closed 7 years ago.
I have created a void extension method which can be used with decimal data types. I wanted to be able to modify the this parameter variable inside the scope of the method. This is the code for my extension method:
public static void SetAndConvertIfHasValue(this decimal assignTo, double? valueToAssign)
{
if (valueToAssign.HasValue)
assignTo = (decimal)valueToAssign.Value;
else
assignTo = 0m;
}
However, when I call it:
data.MyDecimalToSet.SetAndConvertIfHasValue(nullableDouble);
data.MyDecimalToSet is not set to the value in nullableDouble if it has one.
In debug if I step into the extension method, assignTo is changed to the correct value, this change just doesn't bubble up to data.MyDecimalToSet.
At this point I have decided to use a standard method rather than an extension method as a solution to this problem, however I was curious as to why this doesn't work? And whether there is a way around it, or if it simply is impossible?
This doesn't work because when you pass a variable to a method, a copy of that variable is passed. When you operate on that copy, you will only be changing the copy.
Note that this will happen regardless of whether the variable is a reference or a value type. However, if you pass a reference type, you can change the contents of the reference type, since the reference passed to the method will be copied, but it will still reference the original object.
(decimal is a value type, so that last point does not apply to it.)
You are doing it the right way if you use a standard method instead.
This question already has answers here:
How can there be ambiguity between a property getter and a method with one argument?
(4 answers)
Closed 9 years ago.
I have a class which contains a property:
public bool IsMandatory {get;set;}
Now I am adding a method IsMandatory(string str).
public bool IsMandatory(string str)
{
//return false;
//return true;
}
I am getting a compile time error that
the type already contains a definition for 'IsMandatory'
Can't a method name and property name be same in C# ? We use a method and property in different way, why is this giving compile error ?
It's a compiler error because it would cause confusion if the names could be the same. There are some cases where ambiguity could result - for example, when using Action delegates and so on, where methods do not need to have parenthesis, and when using var.
In short: It is just not allowed. Member names (field, property, and method) must be
unique.
This question already has answers here:
Setting the default value of a C# Optional Parameter
(3 answers)
Passing an empty array as default value of an optional parameter [duplicate]
(3 answers)
Closed 9 years ago.
The below code would be quite cool if it worked. However, I can't get it to compile, so I'm assuming this isn't going to work in any form?
public void foo(char[] bar = new char[]{'a'})
{
}
The next-best option is to just do
public void foo(char[] bar = null)
{
if (bar==null)
bar = new {'a'};
}
No it's not possible. The default value needs to be a compile-time constant. The default value will be inserted into the caller, not the callee. Your code would be a problem if the caller has no access to the methods used to create your default value.
But you can use simple overloads:
public void foo(char[] bar)
{
}
public void foo()
{
foo(new char[]{'a'});
}
No, because optional parameter default values need to be constant.
Why do optional parameters in C# 4.0 require compile-time constants?
That will never work because char[] is not a value type but rather a reference type. Only value types can have constants assigned to them in optional parameters. You cannot have a reference to an object (such as an array) at compile time. (Null is the only valid value for an optional reference type.)
Other comments apply as well, but also consider that, since the default value is inserted into the caller at compile time, changing the default value at some later date will not change the value in the caller code (assuming it's being called from another assembly.) Because of this, what you propose as a work-around, or next-best option, is in fact a better practice.
only with value types you have the possibility to set the default value of a parameter to compile-time constants (making it optional). For reference types, only strings have that ability. Other types can only be set to null.
edit: thanks #Martinho Fernandes for pointing that out. For value types, only compile-time constants are allowed
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.