1

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!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ODM4286
  • 11
  • 2
  • Class is the most common data type used in c# as struct in C and it is passed by reference. There is nothing extra you need to do in c# to achieve this. Just create class as you need, create objects of class and pass them in methods. – Chetan Jan 18 '21 at 01:30
  • Learn up Object Oriented Programming. In OOP, the class (a "structure") encapsulates not just the data, but also the functions of how to access that data. Memory is mapped in a pretty similar way under the hood, you just don't get to do silly pointer arithmetic directly. – Charlieface Jan 18 '21 at 01:33
  • In C# you do not have to worry too much about the memory layout, pointers and similar factors. You do not even have to worry about unused memory as it will be automatically collected by the GC – 0___________ Jan 18 '21 at 01:34
  • 4
    @ChetanRanpariya you are confusing "pass by reference" and "reference types". Everything is passed by value by default in C#. – Aluan Haddad Jan 18 '21 at 01:58
  • 5
    I recommend reading [this answer](https://stackoverflow.com/a/8708674/3181933). – ProgrammingLlama Jan 18 '21 at 02:09
  • @AluanHaddad I am curious... If I pass a class object to a method and method changes the state of that object, it is reflected in the original object which was created and passed by the caller to the method... does it not a pass by reference? I know class is a reference type but why this can be not called as pass by reference? – Chetan Jan 18 '21 at 02:30
  • 1
    @ChetanRanpariya Because the "value of the reference" (i.e. a copy of the reference) is passed in, not the actual reference. [Here's an example](https://dotnetfiddle.net/vGeU5F) – NPras Jan 18 '21 at 02:58
  • Short answer for a C dev; all classes are allocated on the heap. All class variables are pointers. Struct variables are not pointers, they are basically C structs. In/Out/Ref keywords force an argument to be passed as a pointer. – Jeremy Lakeman Jan 18 '21 at 03:17
  • Adding to the short comment above, think of it as such: every time you `new` an object, it will perform a `malloc`. The Garbage Collector (GC) will then keep track of this memory and will `realloc` as necessary; and when nothing else in the system references this memory, it will `free` it for you. 99% of the time you shouldn't need to think about it. But as it does in C, the cost of memory operations are not trivial. if you're in a tight loop of a gazillion reps, you could keep `new`ing objects, and the GC will clean them up, but you're putting undue pressure on it. – NPras Jan 18 '21 at 03:40
  • Does this answer your question? [Value and Reference types confusion](https://stackoverflow.com/questions/45589783/value-and-reference-types-confusion) –  Jan 18 '21 at 04:56
  • @ODM4286 You can take a look at these articles: [Value and reference types in .Net (InfoWorld)](https://www.infoworld.com/article/3043992/a-deep-dive-value-and-reference-types-in-net.html) and [Value Type and Reference Type (TutorialsTeacher)](https://www.tutorialsteacher.com/csharp/csharp-value-type-and-reference-type) and [C# Concepts: Value vs Reference Types (Albahari)](http://www.albahari.com/valuevsreftypes.aspx). –  Jan 18 '21 at 04:58
  • @ODM4286 In summary: a reference type (class as well as string) is used as a hidden pointer to forget to manage it; and a value type (struct as well as int, bool, double...) is the same thing (in IL it is also a memory pointer, to the structure as for class) but it is managed as an integral value and passing them in parameters you got a copy, otherwise you must use the ref keyword to pass the pointer. Also value types are differently managed on assignments and therefore in collections. –  Jan 18 '21 at 05:05
  • Great, all of this is helpful. I have a specific example I am still struggling to understand. Let's say I have form1 which instantiates form2. Form2 then instantiates form3. I would like to pass data from form3 to form1. Is this only possible through form2 or is there a more direct method? – ODM4286 Jan 19 '21 at 02:09

1 Answers1

0

In C#, you have a special ref keyword

void foo(ref int x)
{
    x = x + 45;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0___________
  • 60,014
  • 4
  • 34
  • 74
  • Great, all of this is helpful. I have a specific example I am still struggling to understand. Let's say I have form1 which instantiates form2. Form2 then instantiates form3. I would like to pass data from form3 to form1. Is this only possible through form2 or is there a more direct method? – ODM4286 Jan 19 '21 at 01:39