C# out struct parameter

I have this code in C# but I have a problem whit this code:

struct myStruct
{
  public string sOne;
  public string sTwo;
}

public static int ChangeStruct(out myStruct[] arrmyStruct)
{
  arrmyStruct= new myStruct[256];
  arrSNChildrenStruct[0].sOne= "";
  arrSNChildrenStruct[0].sTwo= "";

  return 0;
}

But when I build, I have this error: Inconsistent accessibility: parameter type 'out ........ is less accessible than method .....

What's wrong? Thanks

Jon Skeet
people
quotationmark

This has nothing to do with it being an out parameter, or an array. You'd get the same error with:

public static void ChangeStruct(myStruct foo)

Your method is public, but your struct is internal (the default accessibility for any top-level type) or private if it's a nested type. That means that any caller external to your assembly should have access to the method... but can't possibly understand the method signature. C# doesn't allow you to declare methods which refer to types which can't be seen by all possible callers.

Options:

  • Make the method internal or private
  • Make the struct public

Other notes:

  • Your naming is very unconventional. Name your types according to their meaning, and make it PascalCased. Drop the "arr" prefix from your variable names.
  • Public fields are usually a bad idea, as are mutable structs.

people

See more on this question at Stackoverflow