简体   繁体   中英

How do you pass by reference in C#?

I am coming from an embedded C background and I am trying to learn C#. I often find it difficult to grasp how exactly I can pass data around between classes and forms by reference. Coming from C, I am used to the ability to have a structure that encapsulates all needed data for a set of functions and being able to pass in this data to functions as needed. I can't even begin to think of how I would use a similar technique in C#.

I am finding it very tough to understand how exactly memory is "mapped" in the C# enviroment. For example, I've seen that you can use application.openforms to find a form that may be open but it isn't always clear how to retrieve data from a control on this form.

Lastly, I could use some advice on best practices for creating new instances of forms. Is it best to do that as needed or when my first form loads? I've just learned how to handle events effectively so if I can learn how to manage data across my program more effectively I should be able to start working on some ideas I have. I'd feel much more comfortable if I just had pointers to work with!

Any help is appreciated, thanks!

In C#, you have a special ref keyword

void foo(ref int x)
{
    x = x + 45;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM