Junior — Middle
Kas on võimalik sisestada konstruktor konkreetse andmestruktuuri?
sobes.tech AI
Vastus AI-lt
In C#, a struct can contain constructors, but with some limitations. Specifically, you can define a custom constructor with parameters, but you cannot define a parameterless constructor (the default constructor with parameters is created automatically and cannot be overridden).
Example:
public struct Point
{
public int X;
public int Y;
public Point(int x, int y)
{
X = x;
Y = y;
}
}
Therefore, it is possible to implement a constructor in a struct, but the parameterless constructor cannot be overridden, and all fields must be initialized in custom constructors.