You can do this via its constructor
class testt
{
data d;
public testt()
{
d = new data(); // not needed as it's a struct
d.x =10;
}
}
Usage
var test = new testt();
Note : there is a lot to be desired with your current code.
- You should use standard naming conventions and casing.
- Use self descriptive names.
- Use explicit access modifiers (i.e
public
/ private
/ etc).
- Fields should not be public unless you have a specific use case for it, and should likely be properties.
- You should only use struct if you absolutely know you need them, and prefer classes.
- If you do use a struct they should be immutable.
Nonsensical example
// immutable read-only struct
public readonly struct DogData
{
// public property
public int Length { get;}
public DogData(int length)
{
Length = length;
}
}
public class Dog
{
// private field
private DogData _dogData;
// constructor
public Dog()
{
_dogData = new DogData(10);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…