Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

C# Method Return Structure by Reference

Avatar of Cyber-Storm
Cyber-Storm asked on
6 Comments1 Solution996 ViewsLast Modified:

Is it possible to return a structure by reference?

public struct myStruct
string foo;
string bar;

public myStruct[] Instances;
Instances = new myStruct[1];
Instances[0].foo = "Hello";
Instances[1].bar = "World";

public myStruct getInstance()
return Instances[0];

myStruct instance = getInstance();

This will return a copy of the structure, I want it to return the reference to it so I can do this:
myStruct instance = getInstance();
instance.foo = "Source";
instance.bar = "Code";

I know I can pass instance as a reference variable:
myStruct instance = new MyStruct();
getInstance(ref instance);

But I don't want to do this if I don't have to.

Many thanks,
Avatar of Avodah
AvodahFlag of Western Sahara imageServes the Most High - Not Religion

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answers