I have a modifying method with a signature like
private bool Modify(ref MyClass obj);
that will make modifications to obj
and indicate succes with it's return value. Modify
is not reassigning the reference (I know that this wouldn't work), just modifying instance fields, so I want to use it to do something like the following:
foreach(MyClass obj in myList)
{
bool success = Modify(obj);
// do things depending on success
}
I am running into a problem compiling as obj
is "not being passed with the ref
keyword". However, if I put the ref keyword in like so:
bool success = Modify(ref obj);
I get "cannot use obj
as a ref
/out
because it is a 'foreach iteration variable". I understand that foreach uses an immutable iterator and that's why this doesn't work.
My question is what is the easiest alternative to make something like this work?
I have tried using
foreach(int i = 0; i < myList.Count; i++)
{
bool success = Modify(ref myList[i]);
// do things depending on success
}
but they I get "a property or indexer may not be passed as an out of ref parameter".
Thanks your help.
The every () function is a good alternative to forEach, let us see an example with a test implementation, and then let's return out of the every () function when a certain condition meet. The some () function will test all elements of an array but only one element must pass the test thus making it a good candidate as an alternative to forEach.
The 6th edition of JavaScript is popularly known as ES6 or ECMAScript. ES6 gave JavaScript some of the new, modern and powerful methods, and one such method is .forEach (). This method is used to loop through all the elements of an array and at the same time execute a provided function for every array’s element.
With method chaining becoming almost second nature for arrays in JavaScript, it just reads better to run through an array using a forEach () loop instead of a for loop. It should also be noted that cases where your input size is extremely large, as in the example above, tend to be unlikely.
ES6 gave JavaScript some of the new, modern and powerful methods, and one such method is .forEach (). This method is used to loop through all the elements of an array and at the same time execute a provided function for every array’s element.
Any type within C# is passed actually by value. When you pass an instance of a class to a method what is actually passed is not the instance itself but a reference to it which itself is passed by value. So effectivly you're passing instances of a class as reference - which is why you call them reference-types.
In your case you just modify an existing instance referenced by that reference-value in your method, no need to use the ref
-keyword.
foreach(var m in myList)
{
MyMethod(m);
}
MyMethod(MyClass instance)
{
instance.MyProperty = ...
}
If you'd really pass the reference by reference you'd re-assign the obj
on every iteration within your loop which isn't allowed within a foreach
-block. This would be similar to the following:
foreach(var m in myList)
{
m = new MyClass();
}
On the other side you could also use a classic for-loop. However you'd need a temporary variable to store the outcome of your method:
for(int i = 0; i < myList.Length; i++)
{
var tmp = myList[i];
MyMethod(ref tmp);
myList[i] = tmp;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With