0
0
C Sharp (C#)programming~5 mins

Struct declaration and behavior in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a struct in C#?
A struct is a value type that can hold data and related functionality. It is used to create small data structures that have value semantics.
Click to reveal answer
intermediate
How do structs differ from classes in C#?
Structs are value types stored on the stack, while classes are reference types stored on the heap. Structs do not support inheritance but can implement interfaces.
Click to reveal answer
intermediate
Can a struct have a parameterless constructor in C#?
No, structs cannot have explicit parameterless constructors. The compiler provides a default one that initializes all fields to their default values.
Click to reveal answer
beginner
What happens when you assign one struct variable to another?
The entire data is copied. Each struct variable has its own copy of the data, so changes to one do not affect the other.
Click to reveal answer
intermediate
Why might you choose a struct over a class?
Use a struct when you need a small, lightweight object with value semantics, such as points, colors, or complex numbers, to avoid heap allocation and improve performance.
Click to reveal answer
Which of the following is true about structs in C#?
AStructs are reference types.
BStructs support inheritance from other structs.
CStructs can have explicit parameterless constructors.
DStructs are value types and stored on the stack.
What happens when you pass a struct to a method?
AA copy of the struct is passed.
BA reference to the struct is passed.
CThe struct is converted to a class.
DThe struct is passed as null.
Which keyword is used to declare a struct in C#?
Aclass
Bstruct
Crecord
Dinterface
Can a struct implement interfaces in C#?
AOnly if the interface has no methods.
BNo, structs cannot implement interfaces.
CYes, structs can implement interfaces.
DOnly if the struct is declared as partial.
Which of these is a reason to use a struct instead of a class?
AYou want to avoid heap allocation for small data.
BYou want reference semantics.
CYou need inheritance.
DYou want to use polymorphism.
Explain how structs behave differently from classes in C# regarding memory and copying.
Think about where data is stored and what happens when you assign one variable to another.
You got /4 concepts.
    Describe when and why you would choose to use a struct instead of a class in your C# program.
    Consider the size and behavior of the data you want to represent.
    You got /4 concepts.