Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you have a class in a struct?

Is it possible in C# to have a Struct with a member variable which is a Class type? If so, where does the information get stored, on the Stack, the Heap, or both?

like image 417
Mark T Avatar asked Sep 16 '08 01:09

Mark T


People also ask

Can a struct inherit a class?

A struct cannot inherit from another kind of struct, whereas classes can build on other classes. You can change the type of an object at runtime using typecasting. Structs cannot have inheritance, so have only one type. If you point two variables at the same struct, they have their own independent copy of the data.

Can a class be a member of a struct C++?

The C++ class is an extension of the C language structure. Because the only difference between a structure and a class is that structure members have public access by default and class members have private access by default, you can use the keywords class or struct to define equivalent classes.

Why do we need class if we have struct?

Using a struct we can achieve all the functionality of a class : constructors (that can be modified/overloaded), destructors (that can be modified/overloaded), operator overloading, instance methods, static methods, public / private / protected fields/methods.

Can a struct and a class have the same name?

There is nothing wrong with the code because declaration != definition: You can declare a type as a class , struct or union and then use a different one for the definition. Multiple definitions shouldn't compile; multiple declarations however is just fine.


2 Answers

Yes, you can. The pointer to the class member variable is stored on the stack with the rest of the struct's values, and the class instance's data is stored on the heap.

Structs can also contain class definitions as members (inner classes).

Here's some really useless code that at least compiles and runs to show that it's possible:

using System;  namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {             MyStr m = new MyStr();             m.Foo();              MyStr.MyStrInner mi = new MyStr.MyStrInner();             mi.Bar();              Console.ReadLine();         }     }      public class Myclass     {         public int a;     }      struct MyStr     {         Myclass mc;          public void Foo()         {             mc = new Myclass();             mc.a = 1;         }          public class MyStrInner         {             string x = "abc";              public string Bar()             {                 return x;             }         }     } } 
like image 74
Eric Z Beard Avatar answered Sep 22 '22 19:09

Eric Z Beard


The class content gets stored on the heap.

A reference to the class (which is almost the same as a pointer) gets stored with the struct content. Where the struct content is stored depends on whether it's a local variable, method parameter, or member of a class, and whether it's been boxed or captured by a closure.

like image 34
Ben Voigt Avatar answered Sep 20 '22 19:09

Ben Voigt