-4

I made a method that have one parameter and iside it there is one member when i call this method in the main() and path a parameter to it with the ref keyword i recive error massage "argument 1 may not be passed with ref keword "

i tried to remove the ref keyworf it works well

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace learncsharp
{
    class Program
    {
        static int Value(int x)
       {
           x = 100;
           return x;
       }
        public static void Main()
        {
            int z = 10;
            Value(ref z);
            Console.Read();
        }
    }

 }

i am expecting to get the sam result as to get 10

  • 2
    the `x` parameter isn't declared as `ref` – Daniel A. White May 15 '19 at 15:28
  • 1
    In order to *pass* something by-ref, the parameter needs to be declared as such, i.e. `static int Value(ref int x)` - but... that seems an unlikely thing to want to do here? – Marc Gravell May 15 '19 at 15:29
  • 1
    You need to add that ref to the method also – Hans Kesting May 15 '19 at 15:29
  • Possible duplicate of [Why use the 'ref' keyword when passing an object?](https://stackoverflow.com/questions/186891/why-use-the-ref-keyword-when-passing-an-object) – Shekhar Pankaj May 15 '19 at 15:31
  • The [official documentation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref) clearly defines how to use the `ref` keyword. Please look through these docs before asking on Stack Overflow. – Callum Watkins May 15 '19 at 16:03
  • 2
    @MinaShaker the reason your question was downvoted was due to the lack of effort to solve the problem using the abundance of existing resources on the Internet, such as those that I have directed you to. The first result in Google for "argument 1 may not be passed with ref keyword" is the official documentation, and the second is a [Stack Overflow Q/A](https://stackoverflow.com/questions/41777751/i-am-getting-an-error-argument-2-may-not-be-passed-with-ref-keyword-while-using) that fully answers your question. – Callum Watkins May 15 '19 at 16:33
  • Possible duplicate of [i am getting an error Argument 2 may not be passed with ref keyword while using ado.net](https://stackoverflow.com/questions/41777751/i-am-getting-an-error-argument-2-may-not-be-passed-with-ref-keyword-while-using) – Callum Watkins May 15 '19 at 17:06

1 Answers1

1

Parameters defined as 'ref' allow you to see changes from the caller after completion.

Parameters defined as 'out' MUST be set in the function, and their value after completion can be seen.

The declaration of 'ref' or 'out' must be in the function header AND the call to the function.

Otherwise parameters are passed by value, and any changes are lost.

Note that objects passed by value are sharing the same data on the heap, so any change to properties/fields of the object will also be seen by the caller as if they were passed by 'ref'

using your own code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace learncsharp
{
    class Program
    {
        static int Value(ref int x)
       {
           x = 100;
           return x;
       }
        public static void Main()
        {
            int z = 10;
            Value(ref z);
            Console.Read();
        }
    }

 }
Steve Todd
  • 1,250
  • 6
  • 13
  • Some remarks: 1) You are ignoring the return value of the `Value(ref z)` call; 2) Inspect the value of `z` after that call (Console.WriteLine it) - that's the reason to use `ref` – Hans Kesting May 17 '19 at 06:51