简体   繁体   English

C#out结构参数

[英]C# out struct parameter

I have this code in C# but I have a problem whit this code: 我在C#中有此代码,但在此代码中却遇到了问题:

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 ..... 但是,当我构建时,出现以下错误:不一致的可访问性:参数类型'out ........比方法.....的访问性差。

What's wrong? 怎么了? Thanks 谢谢

Make a public struct myStruct instead of internal struct. public struct myStruct而不是内部结构。 Or make ChangeStruct() private if you only use it locally. 如果仅在本地使用,则将ChangeStruct()设为private

This has nothing to do with it being an out parameter, or an array. 这与out参数或数组无关。 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. 您的方法是公共的,但是您的结构internal (任何顶级类型的默认可访问性)或private如果它是嵌套类型)。 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. C#不允许您声明引用所有可能的调用者都看不到的类型的方法。

Options: 选项:

  • Make the method internal or private 将方法设为internalprivate
  • Make the struct public 公开结构

Other notes: 其他说明:

  • Your naming is very unconventional. 您的命名非常不常规。 Name your types according to their meaning, and make it PascalCased. 根据它们的含义命名您的类型,并将其命名为PascalCased。 Drop the "arr" prefix from your variable names. 从变量名中删除“ arr”前缀。
  • Public fields are usually a bad idea, as are mutable structs. 公共字段和可变结构通常都是个坏主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM