[英]How to return Struct From Function in C#?
I defined a struct like this : 我定义了一个像这样的结构:
public struct Averages
{
public decimal Sell_GoldOunce = 0;
public decimal Buy_GoldOunce = 0;
public decimal Sell_SilverOunce = 0;
public decimal Buy_SilverOunce = 0;
public int Sell_Mazene = 0;
public int Buy_Mazene = 0;
public int Sell_Gram_18 = 0;
public int Buy_Gram_18 = 0;
public int Sell_Gram_24 = 0;
public int Buy_Gram_24 = 0;
};
Now i Want to Use it in my Function And Then RETURN IT 现在我想在我的功能中使用它然后返回它
public (?) AssignValues()// I WANT TO KNOW WHAT SHOULD I PUT INSTITE OF (?)
{
Averages GoldValues;
GoldValues.Sell_GoldOunce = somevalue;
GoldValues.Buy_GoldOunce = somevalue;
GoldValues.Sell_SilverOunce = somevalue;
GoldValues.Buy_SilverOunce = somevalue;
GoldValues.Sell_Mazene = somevalue;
GoldValues.Buy_Mazene = somevalue;
GoldValues.Sell_Gram_24 = somevalue;
GoldValues.Buy_Gram_24 = somevalue;
GoldValues.Sell_Gram_18 = somevalue;
GoldValues.Buy_Gram_18 = somevalue;
return GoldValues;
}
as i said i want to know what kind i should define my function to can return struct 正如我所说,我想知道我应该定义我的函数以返回struct
Add the name of your struct: 添加结构的名称:
public Averages AssignValues()
{
Averages GoldValues = new Averages();
GoldValues.Sell_GoldOunce = somevalue;
GoldValues.Buy_GoldOunce = somevalue;
GoldValues.Sell_SilverOunce = somevalue;
GoldValues.Buy_SilverOunce = somevalue;
GoldValues.Sell_Mazene = somevalue;
GoldValues.Buy_Mazene = somevalue;
GoldValues.Sell_Gram_24 = somevalue;
GoldValues.Buy_Gram_24 = somevalue;
GoldValues.Sell_Gram_18 = somevalue;
GoldValues.Buy_Gram_18 = somevalue;
return GoldValues;
}
public Averages AssignValues()
write it, you can return Structs just like classes, but remember that fields in structs are initalized with default values, so your definition of struct should be: 写它,你可以像类一样返回Structs,但是请记住结构中的字段是使用默认值初始化的,所以你对struct的定义应该是:
public struct Averages
{
public decimal Sell_GoldOunce;
public decimal Buy_GoldOunce;
public decimal Sell_SilverOunce;
public decimal Buy_SilverOunce;
public int Sell_Mazene;
public int Buy_Mazene;
public int Sell_Gram_18;
public int Buy_Gram_18;
public int Sell_Gram_24;
public int Buy_Gram_24;
};
So when you write Avereges a = new Avereges()
-> a.Buy_Gram_24
will be 0 because thats int's default value. 所以当你写
Avereges a = new Avereges()
- > a.Buy_Gram_24
将为0,因为这是int的默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.