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

前端 未结 5 1469
孤独总比滥情好
孤独总比滥情好 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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.

提交回复
热议问题