adspace
What is the difference between struct and class c#?
Answer Posted / Avneesh Sharma
In C#, both Struct (struct) and Class are user-defined data types. However, there are some differences:
1. By default, classes are reference types, while structs are value types.
2. Classes support inheritance with ':' operator, but structs do not. The ':' operator in structs is used for indexer declarations.
3. Classes can have a constructor, whereas structs are instantiated using the default constructor unless explicitly defined.
4. Struct fields are public by default and cannot be private or protected, while class fields can be any access modifier (public, private, protected).
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers