简体   繁体   English

尝试制作 c# 库(.dll)时出现变量问题

[英]Problem with variables while trying to make a c# library(.dll)

I want to make a library that sums 2 numbers(a and b) and then stores the value into a result variable inside a library(.dll).我想创建一个将 2 个数字(a 和 b)相加的库,然后将值存储到库(.dll)内的结果变量中。 I tried this:我试过这个:

 public static void Sum(int number1, int number2, int result)
    {   
        result = number1 + number2;
        
    }

but I can't make it so that in a program that uses this library, you can get the value of the result value that this function calculates and that's what I couldn't figure out for the past days.但我无法做到,因此在使用此库的程序中,您可以获得此 function 计算的结果值的值,这是我过去几天无法弄清楚的。 If you need any more info I will gladly provide it to you!如果您需要更多信息,我很乐意提供给您! Hope someone can help me!希望可以有人帮帮我!

In your case you do not return any result, consider using some of the following:在您的情况下,您不返回任何结果,请考虑使用以下一些:

    public static void Main()
    {
        var sumRes = SumResut(1, 2);
        Console.WriteLine($"SumResult = {sumRes}");

        OutSum(1, 2, out int outRes);
        Console.WriteLine($"OutSum = {outRes}");

        int refRes = 0;
        RefSum(1, 2, ref refRes);
        Console.WriteLine($"RefSum = {refRes}");
    }

    public static int SumResut(int number1, int number2)
    {
        return number1 + number2;
    }

    public static void OutSum(int number1, int number2, out int result)
    {
        result = number1 + number2;
    }

    public static void RefSum(int number1, int number2, ref int result)
    {
        result = number1 + number2;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM