Answers for "can you stroe a refernce in c#"

C#
1

can you stroe a refernce in c#

// As in having a pointer/reference to this NON object you can make a class for
// it which will be shown down below but on its own a non-object can't have a 
// stored reference outside functions like this:
public void Myfunction(ref int myInt)
{
  	//sets myInt to 2
  	myInt = 2;
}

// Or this

public ref int Myfunc(ref int x)
{
  return ref x;// x can only be passed like this if its a reference parameter
}

// On the other hand there are many types that have allready got a pre made 
// struct like intPtr which functions as a pointer for ints.
// So be shure to do research if you are using a predefined non-Object like
// int or ushort or even string (which can be done by using a intPtr but hey
// its a solution) to see if it has an equivilent struct already made.
// If this is your own non object though it is better to make a class 
// for it like this becuase Objects on the other hand can only be stored
//(and passed) by reference so if you make a class like this:

public class IntReference
{
  	public int MyInt;
  	public IntReference()
    {
      MyInt = 0;
    }
  	public IntReference(int val)
    {
      MyInt = val;
    }
}

// you can store the class IntReference as your pointer/reference to MyInt but
// if we are talking about ints specifically there is a struct called intPtr
// which is a platform specific pointer to an int which can be used instead.
Posted by: Guest on March-25-2021

Code answers related to "can you stroe a refernce in c#"

C# Answers by Framework

Browse Popular Code Answers by Language