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

前端 未结 5 1465
孤独总比滥情好
孤独总比滥情好 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: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
    }
    

提交回复
热议问题