你好,我很难搞清楚这一点.我有这些结构和类.
struct Circle
{ ... }
class Painting
{
List<Circle> circles;
public List<Circle> circles
{
get { return circles; }
}
}
Run Code Online (Sandbox Code Playgroud)
我试图使用以下代码从外部修改绘画类中的一个圆圈:
MutatePosition(ref painting.Circles[mutationIndex], painting.Width, painting.Height);
Run Code Online (Sandbox Code Playgroud)
这行给了我一个编译器错误:
属性,索引器或动态成员访问不能作为out或ref参数传递
为什么会这样,如果不过多地改变我的代码,我该怎么做才能解决它?
C#:函数中的'out'out'参数是对象属性/变量吗?
例如:
我可以调用函数如下:
someFunction(x, y, out myObject.MyProperty1)
Run Code Online (Sandbox Code Playgroud) 我想传递一个类属性(或getter/setter,如果需要)引用一个函数.
例如,我有一个包含许多布尔标志的类数组.
class Flags
{
public bool a;
public bool b;
public bool c;
public string name;
public Flags(bool a, bool b, bool c, string name)
{
this.a = a;
this.b = b;
this.c = c;
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以编写一个方法来返回所选标志为true的所有Flags实例
public Flags[] getAllWhereAisTrue(Flags[] array)
{
List<Flags> resultList = new List<Flags>();
for (int i = 0; i < array.Length; i++)
{
if (array[i].a == true) // for each Flags for which a is true
{ // add it to …Run Code Online (Sandbox Code Playgroud) 当我尝试在C#中调整数组大小时,如下所示,
Array.Resize(ref Globals.NameList, 0);
Run Code Online (Sandbox Code Playgroud)
我得到以下错误
A property or indexer may not be passed as an out or ref parameter
Run Code Online (Sandbox Code Playgroud)
Globals是一个对象.NameList是在Globals类中声明的字符串类型数组.
请帮我通过发布正确的代码来解决这个问题.
谢谢!
我正在尝试为我的模型创建一个通用存储库.目前我有3种不同的型号,它们之间没有任何关系.(联系人,备注,提醒).
class Repository<T> where T:class
{
public IQueryable<T> SearchExact(string keyword)
{
//Is there a way i can make the below line generic
//return db.ContactModels.Where(i => i.Name == keyword)
//I also tried db.GetTable<T>().Where(i => i.Name == keyword)
//But the variable i doesn't have the Name property since it would know it only in the runtime
//db also has a method ITable GetTable(Type modelType) but don't think if that would help me
}
}
Run Code Online (Sandbox Code Playgroud)
在MainViewModel中,我像这样调用Search方法:
Repository<ContactModel> _contactRepository = new Repository<ContactModel>();
public void …Run Code Online (Sandbox Code Playgroud)