如何在 C# 中从函数返回结构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18890152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:31:17  来源:igfitidea点击:

How to return Struct From Function in C#?

c#functionmethodsstructreturn-value

提问by Amin AmiriDarban

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

正如我所说,我想知道我应该定义我的函数以返回结构的类型

采纳答案by Kamil Budziewski

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:

编写它,您可以像类一样返回结构,但请记住,结构中的字段使用默认值进行初始化,因此您对结构的定义应该是:

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_24will be 0 because thats int's default value.

所以当你写Avereges a = new Avereges()->a.Buy_Gram_24将是 0 因为那是 int 的默认值。

回答by Andrei Neculai

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;
}