Why am I getting these out parameter errors in C#?

前端 未结 5 1464
孤独总比滥情好
孤独总比滥情好 2020-12-19 02:04

I am new to C#. I\'ve tried this with out parameter in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Firs         


        
相关标签:
5条回答
  • 2020-12-19 02:09

    As of C# 7.0 the ability to declare a variable right at the point where it is passed as an out argument was introduced.

    Before:

    public void PrintCoordinates(Point p)
    {
        int x, y; // have to "predeclare"
        p.GetCoordinates(out x, out y);
        WriteLine($"({x}, {y})");
    }
    

    C# 7.0

    public void PrintCoordinates(Point p)
    {
        p.GetCoordinates(out int x, out int y);
        WriteLine($"({x}, {y})");
    }
    

    You can even use var key word:

    p.GetCoordinates(out var x, out var y);
    

    Be carefull with the scope of out parameter. For example, in the following code, "i" is only used within if-statement:

    public void PrintStars(string s)
    {
        if (int.TryParse(s, out var i)) { WriteLine(new string('*', i)); }
        else { WriteLine("Cloudy - no stars tonight!"); }
    }
    

    Further information can be found here. This link is not only about out parameter, but all the new features introduced in c# 7.0

    0 讨论(0)
  • 2020-12-19 02:14
    public void Ref_Test(ref int x)
    {
        var y = x; // ok
        x = 10;
    }
    
    // x is treated as an unitialized variable
    public void Out_Test(out int x)
    {
        var y = x; // not ok (will not compile)
    }
    
    public void Out_Test2(out int x)
    {
        x = 10;
        var y = x; // ok because x is initialized in the line above
    }
    
    0 讨论(0)
  • 2020-12-19 02:25

    You're getting an error because a variable sent to a method as an out parameter does not have to be initialized before the method call. The following is 100% correct code:

    class Program
    {
        static void Main(string[] args)
        {
            First f = new First();
            int x;
            f.fun(out x);
        }
    }
    

    Looks like you're looking for ref instead of out here:

    class First
    {
        public void fun(ref int m)
        {
            m *= 10;
            Console.WriteLine("value of m = " + m);
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            First f = new First();
            int x = 30;
            f.fun(ref x);
        }
    }
    
    0 讨论(0)
  • 2020-12-19 02:27

    out parameters are for when the function wants to pass a value out of itself. What you want here is ref, which is when the function expects it to be passed in, but can change it.

    For examples of how both are supposed to be used, read http://www.dotnetperls.com/parameter. It's explained in simple terms, and you should be able to get a good understanding of it.

    You should note that in your code, you never access the variable after the function call, therefore ref doesn't actually do anything. Its purpose is to send changes back to the original variable.

    0 讨论(0)
  • 2020-12-19 02:28

    ref means that you are passing a reference to the variable that has been declared and initialized, before calling the method, and that the method can modify the value of that variable.

    out means you are passing a reference to the variable that has been declared but not yet initialized, before calling the method, and that the method must initialize or set it's value before returning.

    0 讨论(0)
提交回复
热议问题