Home > Blockchain >  Passing a C# Dictionary as a function parameter to allocate its memory does not work
Passing a C# Dictionary as a function parameter to allocate its memory does not work

Time:02-11

I have below code

using System;
using System.Collections.Generic;
class Program {
    static void add(Dictionary<string,string> hello){
      hello = new Dictionary<string,string>();
      hello["hello"] = "hello";
    }
    static void Main(string[] args) {
      Employee employee = new Employee(); // Employee class
      
      add(employee.hello);     //  employee.hello is property like public Dictionary<string,string> hello {get;set;}
        Console.WriteLine(hello["hello"]);
    }
}

I have also tried using ref but i'm getting A property or indexer may not be passed as ref

When I intialize hello this program works fine but when i try to intialize it in function it is giving me null reference error. So is there any way to initialize dictionary by passing through a function like that ?

CodePudding user response:

Have you considered passing in the Employee object instead of one of its properties? Something like...

static void add(Employee emp) {
  emp.hello = new Dictionary<string, string>();
  emp.hello["hello"] = "hello";
}


static void Main(string[] args) {
  Employee employee = new Employee(); // Employee class
  add(employee);
  Console.WriteLine(employee.hello["hello"]);
}

CodePudding user response:

You need to chose if you are passing in a created dictionary or creating one in the add method.

Here's option 1:

class Program
{
    static void add(Dictionary<string, string> hello)
    {
        hello["hello"] = "hello";
    }
    
    static void Main(string[] args)
    {
        Employee employee = new Employee();
        add(employee.hello);
        Console.WriteLine(employee.hello["hello"]);
    }
}

public class Employee
{
    public Dictionary<string, string> hello = new Dictionary<string, string>();
}

Here's option 2:

class Program
{
    static Dictionary<string, string> add() =>
        new Dictionary<string, string>()
        {
            { "hello", "hello" },
        };
    
    static void Main(string[] args)
    {
        Employee employee = new Employee();
        employee.hello = add();
        Console.WriteLine(employee.hello["hello"]);
    }
}

public class Employee
{
    public Dictionary<string, string> hello = null;
}

And, option 3, also works.

class Program
{
    static void add(ref Dictionary<string, string> hello)
    {
        if (hello == null)
        {
            hello = new Dictionary<string, string>();
        }
        hello["hello"] = "hello";
    }
    
    static void Main(string[] args)
    {
        Employee employee = new Employee();
        add(ref employee.hello);
        Console.WriteLine(employee.hello["hello"]);
    }
}

public class Employee
{
    public Dictionary<string, string> hello = null;
}

All three of those print hello to the console.

It's a little unclear what you're trying to do. But if it is the last one then you're mixing up the responsibilities of your classes and methods.

The class Employee should be responsible for setting up its own fields.

Finally, it would be useful for you to use the C# variable and method naming standards.

  • Related