namespace MyNamespace
{
public struct MyStruct
{
public string MyString;
public int MyInt;
public bool MyBool;
}
public class MyClass
{
private List<MyStruct> MyPrivateVariable;
public List<MyStruct> MyVariable
{
get
{
if (MyPrivateVariable == null)
{
MyPrivateVariable = new List<MyStruct>();
MyPrivateVariable.Add(new MyStruct());
MyPrivateVariable.Add(new MyStruct());
}
return MyPrivateVariable;
}
}
public void MyLoop()
{
foreach (MyStruct ms in MyVariable)
{
// Doesn't compile, but it works if you execute it through the Immediate window, or in Quickwatch
ms.MyBool = false;
// Compiles, …Run Code Online (Sandbox Code Playgroud) 我需要有关结构初始化的数组的帮助.在下面的代码中,我们如何完成注释中定义的初始化?
class structExample
{
struct state{
int previousState;
int currentState;
}
static state[] durum;
public static void main(String[] args)
{
durum = new state[5];
// how we can assign new value to durum[0].previousState = 0; doesn't work ??
}
}
Run Code Online (Sandbox Code Playgroud)
}
谢谢..
我目前正在开发一个C#项目,我需要将数据添加到自定义List数组中.然后,我需要枚举列表,查找特定记录然后修改数据.但是,我在Visual Studio中收到错误
Cannot modify the return value of System.Collections.Generic.List<EmailServer.ThreadCheck>.this[int] because it is not a variable.
Run Code Online (Sandbox Code Playgroud)
下面是初始化List Array的代码
static List<ThreadCheck> threadKick = new List<ThreadCheck>();
Run Code Online (Sandbox Code Playgroud)
下面是我如何将数据添加到List数组
public static void addThread(ILibraryInterface library, int threadID, string threadName)
{
if (Watchdog.library == null)
{
Watchdog.library = library;
}
long currentEpochTime = library.convertDateTimeToEpoch(DateTime.Now);
threadKick.Add(new ThreadCheck()
{
threadName = threadName,
threadID = threadID,
epochTimeStamp = currentEpochTime
});
library.logging(classDetails + MethodInfo.GetCurrentMethod().Name, string.Format("Thread ID: {0} has been added to watchdog for {1}",
threadID, threadName));
}
Run Code Online (Sandbox Code Playgroud)
下面是我试图修改列表中的数据的代码
public static void …Run Code Online (Sandbox Code Playgroud) 我有给定的listitem类:
class Vector
{
public int Column { get; set; }
public int Row { get; set; }
public int TableID { get; set; }
public Vector(int column, int row, int tableID)
{
TableID = tableID;
Row = row;
Column = column;
}
}
Run Code Online (Sandbox Code Playgroud)
后来我有这个项目的类型列表,我想知道给定的向量(列,行,表)是否已添加到此列表中.当然是琐碎的解决方案:
var items = new List<Vector>();
items.Add(new Vector(1, 2, 3));
items.Add(new Vector(5, 6, 7));
for (int i = 0; i < 1000; i++)
{
if (items.Any(e => e.Column == 1 && e.Row == 2 && e.TableID …Run Code Online (Sandbox Code Playgroud) 我正在看这个问题,除了一种奇怪的方式来枚举某些东西之外,op也遇到了麻烦,因为枚举器是一个结构.我知道返回或传递一个struct会使用一个副本,因为它是一个值类型:
public MyStruct GetThingButActuallyJustCopyOfIt()
{
return this.myStructField;
}
Run Code Online (Sandbox Code Playgroud)
要么
public void PretendToDoSomething(MyStruct thingy)
{
thingy.desc = "this doesn't work as expected";
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是如果MyStruct实现IMyInterface(例如IEnumerable),这些类型的方法是否会按预期工作?
public struct MyStruct : IMyInterface { ... }
//will caller be able to modify the property of the returned IMyInterface?
public IMyInterface ActuallyStruct() { return (IMyInterface)this.myStruct; }
//will the interface you pass in get its value changed?
public void SetInterfaceProp(IMyInterface thingy)
{
thingy.desc = "the implementing type is a struct";
}
Run Code Online (Sandbox Code Playgroud) 我在c#中的代码有问题,如果有人可以帮我解决我的问题.
在函数中,我正在解析Xml文件并将其保存到结构中.
然后我尝试从具有特定节点ID的所述结构中检索一些信息,并且我的代码失败了
"不能在匿名方法,lambda表达式或查询表达式中使用ref或out参数'c'"
这是我的代码:
public void XmlParser(ref Point a, ref Point b, ref Point c)
{
XDocument xdoc = XDocument.Load(XmlDirPath);
var coordinates = from r in xdoc.Descendants("move")
where int.Parse(r.Attribute("id").Value) == c.NodeID // !! here is the error !!
select new
{
X = r.Element("x").Value,
Y = r.Element("y").Value,
Z = r.Element("z").Value,
nID = r.Attribute("id").Value
};
foreach (var r in coordinates)
{
c.x = float.Parse(r.X1, CultureInfo.InvariantCulture);
c.y = float.Parse(r.Y1, CultureInfo.InvariantCulture);
c.z = float.Parse(r.Z1, CultureInfo.InvariantCulture);
c.NodeID = Convert.ToInt16(r.nID);
}
}
public struct …Run Code Online (Sandbox Code Playgroud) 我有一个结构
public struct card
{
PictureBox picture;
double value;
}
Run Code Online (Sandbox Code Playgroud)
我想制作一个数组,并在我继续时添加/删除图片和值.我无法做到这一点
card[] c = new card[13];
c[1].value = 4;
Run Code Online (Sandbox Code Playgroud)
如何分配,阅读和机会值?
假设以下方法:
int ExtractMedian(int Statistic)
{
return ExtractionWork;
}
Run Code Online (Sandbox Code Playgroud)
有可能强制Statistic只接受奇数,比如1, 3, 5使用ref例如但是在传递后不检查值吗?
我在读虽然关于在计算器上不纯的方法问题,在这里,它让我想起了结构设计的最佳实践.
阅读有关创建不可变结构的示例,此处属性仅定义为getter.
public DateTime Start { get { return start; } }
public DateTime End { get { return end; } }
public bool HasValue { get { return hasValue; } }
Run Code Online (Sandbox Code Playgroud)
其他地方的其他示例包括System.Drawing.Point属性中的getter和setter.
public int Y {
get {
return y;
}
set {
y = value;
}
}
Run Code Online (Sandbox Code Playgroud)
该设计准则不指定,但他们都相当简洁.结构属性的推荐方法是什么?只读或允许写作?
我是一名新的程序员,我的新项目是制作一份排序日记.
我希望日志本身是一个列表,但每个新帖子都是一个新的数组,可以包含2个项目(标题和帖子).我应该补充一点,这是一个学校项目,所以我需要一个正确方向的颠簸.
我希望我能清楚地知道我想做什么,并提前感谢!