c#ef修改指定项
要修改C#中的指定项,可以使用索引器或属性来实现。以下是两种方法:
-
使用索引器:
public class MyClass { private int[] myArray = { 1, 2, 3, 4, 5 }; public int this[int index] { get { return myArray[index]; } set { myArray[index] = value; } } } // 使用索引器修改指定项 MyClass myObject = new MyClass(); myObject[2] = 10; // 将索引为2的项修改为10 -
使用属性:
public class MyClass { private int myItem; public int MyItem { get { return myItem; } set { myItem = value; } } } // 使用属性修改指定项 MyClass myObject = new MyClass(); myObject.MyItem = 10; // 将指定项的值修改为10
无论使用索引器还是属性,都可以通过指定索引或属性名来修改指定项的值
原文地址: https://www.cveoy.top/t/topic/iH83 著作权归作者所有。请勿转载和采集!