Skip to main content
 首页 » 编程设计

c#之如何实例化新的结构对象

2024年02月24日15tintown

这是正确的吗?

//Structure 
public struct Shape 
{ 
    public string mShape; 
    public int mSide; 
 
    public Shape(/*initial sides parameter*/ int sSide, string sShape) 
    { 
        mShape = sShape; 
        mSide = sSide; 
    } 
 
} 
 
//more code here.... but no need to post. 
//Instantiate new Shape structure object 
myShape = new Shape(/*Sides integer value*/ ,sShape); 
 
//How to Add the additional parameter to construct the object? 

请您参考如下方法:

不确定我是否遵循。如果您的问题是,如何在构造函数中发送整数?

给你:

myShape = new Shape(4, sShape); 

或:

int sides = 4; 
myShape = new Shape(sides, sShape);