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

Readonly structs in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a readonly struct named Point.

C Sharp (C#)
public [1] struct Point { public int X; public int Y; }
Drag options to blanks, or click blank then click option'
Areadonly
Bstatic
Csealed
Dabstract
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' or 'sealed' instead of 'readonly'.
2fill in blank
medium

Complete the code to create a readonly property in the readonly struct.

C Sharp (C#)
public readonly struct Rectangle { public int Width { [1]; } }
Drag options to blanks, or click blank then click option'
Aset
Binit
Cget
Dprivate set
Attempts:
3 left
💡 Hint
Common Mistakes
Adding a 'set' accessor to a readonly property.
3fill in blank
hard

Fix the error in the readonly struct method that tries to modify a field.

C Sharp (C#)
public readonly struct Circle { public int Radius; public void SetRadius(int r) { [1] = r; } }
Drag options to blanks, or click blank then click option'
ARadius
Bthis.Radius
Creadonly Radius
Dbase.Radius
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to assign to fields without 'this' or in readonly structs.
4fill in blank
hard

Fill both blanks to create a readonly struct with a constructor initializing its fields.

C Sharp (C#)
public [1] struct Vector2D { public int X; public int Y; public [2] Vector2D(int x, int y) { X = x; Y = y; } }
Drag options to blanks, or click blank then click option'
Areadonly
Bstatic
CVector2D
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' instead of 'readonly' or wrong constructor name.
5fill in blank
hard

Fill all three blanks to create a readonly struct with a method that returns the distance from origin.

C Sharp (C#)
public [1] struct Point3D { public int X; public int Y; public int Z; public double [2]() { return Math.[3](X * X + Y * Y + Z * Z); } }
Drag options to blanks, or click blank then click option'
Areadonly
BDistanceFromOrigin
CSqrt
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' for the struct or wrong method name or math function.