All Questions
Tagged with pass-by-reference c#
390 questions
0
votes
2
answers
101
views
'new' keyword returning the same instance of a class
I have these two classes, one inheriting from the other.
When an external script calls .copy(), it always receives a parent DataComponent, never a BooleanProperty.
If copy() isn't called, a pass-by-...
1
vote
0
answers
62
views
Is it possible to change a value passed by reference within a coroutine?
I'm essentially trying to make a working version of this:
public static Coroutine AnimateValue(this MonoBehaviour behaviour, ref float value, float endValue)
{
return behaviour.StartCoroutine(...
1
vote
1
answer
316
views
Returning a new ref struct instance that holds a reference to the returning instance of another struct
I recently wanted to write code where an instance A of struct S1 returns some instance B of struct S2 that needs some values from instance A. Trivial implementation of struct S2 would be to store ...
2
votes
2
answers
253
views
Why does the "in" keyword allow mutation of properties C#?
I am currently working on a function where an object (of self written type Catalog) is passed and shall not be mutated / changed in the called function. Here a quick example.
public override bool ...
0
votes
1
answer
79
views
C#: How to keep the global variable not changed when passed to a function?
I am developing an add-in for OneNote.
In the sinppet below, a global variable AllPageXML is claimed first.
Then the very first time when a window is loaded, AllPageXML is initinized with a value.
...
2
votes
2
answers
568
views
The new operator return value in C#
In C#, a new operator can be used to init an object of specific type (class). For example, consider we have a Person class, the following code create an object instance from Person class:
Person ...
0
votes
1
answer
902
views
Inline variable declaration workaround for ref parameter
This is purely out of interest. I'd specifically like to know if there is a cleaner way of passing a ref int without having to create a variable.
My current workaround:
ref stackalloc int[1] { 0 }[0]
...
1
vote
1
answer
110
views
Why does it give an error when I call a function as a call by reference with a variable value that has no value?
int x;
Method3(ref x);
Console.WriteLine("x is : {0}", x);
Console.ReadKey();
static void Method3(ref int a)
{
a += 100;
}
I face this error! "Error CS0165 Use of unassigned ...
-2
votes
2
answers
103
views
Unexpected behavior using By Ref with Reference type Dictionary
Experienced an unexpected behavior when using Dictionary type and passing by reference.
Within a nested call, the reference to the underlaying base dictionary seems to be lost or replaced.
All child ...
1
vote
0
answers
260
views
Implement IEquatable for struct, and pass by reference
Is it possible to implement IEquatable from a struct, but pass the compared argument by reference?
Example:
struct BigToCopy : IEquatable<BigToCopy>
{
int x, y, z, w, foo bar, etc;
bool ...
-1
votes
1
answer
134
views
I am trying to change a variable through reference. However the variable changes only once
//This variable I am trying to change
int levelCount = 0;
IEnumerator LoadScene(float waitTime){
yield return new WaitForSeconds(waitTime);
LevelCount(ref levelCount); //through this method ...
0
votes
1
answer
1k
views
Changing value in array of objects
I have a bit of code and I am confused with my observations
var arr = new Class[]
{
new Class{Name = "First"},
new Class{Name = "Second"}
};
var first = arr[0];
arr[0] = ...
0
votes
2
answers
64
views
Implications of pass-by-value vs pass-by-reference in C# [duplicate]
I was following along a tutorial online about C#. The tutorial talks about passing-by-reference vs passing-by-value. What I am confused about specifically is that since in C#, there is a distinction ...
0
votes
1
answer
75
views
Passing an object back to MainWindow from a subwindow
I'm trying to create a little database program in which you can add, modify and remove articles for a news site. There is a MainWindow with a DataGrid to display all the articles. Once the Add button ...
0
votes
0
answers
508
views
Why do i get Argument 1 may not be passed with the 'ref' keyword?
I am trying to pass an string by reference from From1 to Form2, want to modify the string in Form2 and use the altered value in Form1.
This is my code:
Inside Form1.cs:
private void ...