相关疑难解决方法(0)

如何在.NET中修改List <T>的结构?

为什么我无法修改列表项?

struct Foo 
{
    public string Name;
}

Foo foo = new Foo();
foo.Name = "fooNameOne";

List<Foo> foos = new List<Foo>();
foos.Add(foo);

// Cannot modify the return value of 
// 'List<Foo>.this[int]' because it is not a variable   
//foos[0].Name = "fooNameTwo";

Foo tempFoo = foos[0];
tempFoo.Name = "fooNameTwo";

Console.WriteLine(foos[0].Name); // fooNameOne
Run Code Online (Sandbox Code Playgroud)

编辑:
我想离开Foo的结构.我该怎么办?foos[0] = tempFoo?有点复杂只是为了作业?!

.net list

4
推荐指数
1
解决办法
1035
查看次数

我应该使用Struct吗?

可能重复:
何时在C#中使用struct?

嗨,我正在创建一个在C#中有一个纯粹用于保存变量的类的应用程序,除了设置和获取这些变量之外别无其他.我想知道,为了提高效率和良好的编码实践,我是否应该将此类转换为结构,以便正确使用它.我之前从未使用过结构但是一直在研究它们但是我在使用它时遇到了一些麻烦.任何意见,将不胜感激!

谢谢,斯图尔特

c# struct

4
推荐指数
1
解决办法
448
查看次数

C#:我应该使用或参考这个结构吗?

我不确定问这个问题的最好方法,所以我先从一个例子开始:

public static void ConvertPoint(ref Point point, View fromView, View toView) {
    //Convert Point
}
Run Code Online (Sandbox Code Playgroud)

这个调用是递归的.你传入一个点,它相对于它fromView是相对的toView(只要一个是另一个的祖先).

该调用是递归的,一次将该点转换为一个级别.我知道,可变结构是坏的,但我使用可变点的原因是我只需要创建一个单点并将其传递给递归调用,这就是为什么我使用ref.这是正确的方法,还是使用out参数更好,或者只是声明方法返回一个点?

在这些情况下,我不太熟悉如何处理结构体而不是类.这是从Java移植的代码,其中显然必须是一个类,因此有意义的是反复使用相同的临时点而不是创建一个必须在每次调用时被垃圾收集的新点.

这可能是一个令人困惑的问题,并且为了更多的混乱而堆积起来,当我在它时,我应该保留一个临时静态Point实例以进行快速转换,或者只要在调用此方法时创建新点就这么简单(它被称为很多)?

c# performance struct

4
推荐指数
1
解决办法
1943
查看次数

使用向下箭头键滚动的WPF DataGrid行为很奇怪

我有一段时间以来一直使用的WPF DataGrid,效果很好.与其他海报不同,我还没有滚动条或鼠标滚轮的问题.我已CTRLEND编程到DataGrid的末尾,然后它跟踪最近添加的项目.我可以DataGridup键向上滚动内容.

但是,我对down键的行为非常奇怪!如果我从我的顶部开始DataGrid并按住down键,它会滚动一点,然后最终在两个相邻行之间来回反弹.如果我pgdn,它将向下滚动更多,然后跳回到前两行的最顶部,它将跳转到之间,然后向下滚动到我要去的点pgdn.如果我再向下翻页,该down键将滚动到结尾.如果我走到顶部DataGrid并重新开始,我会一遍又一遍地得到完全相同的行为.

我还没有找到解决这个问题的帖子,而且我在DataGrid文档中没有看到任何有用的内容.

它只是一个三列DataGrid,每列显示TextBlocks.任何人都可以解释为什么这种滚动模式有问题吗?这是XAML:

<DataGrid ItemsSource="{Binding MainLog}" AutoGenerateColumns="False" 
     Name="log_datagrid" SelectedCellsChanged="log_datagrid_SelectedCellsChanged"   
     KeyUp="datagrid_KeyUp" LoadingRow="log_datagrid_LoadingRow">
    <DataGrid.Columns>
        <!-- timestamp -->
        <DataGridTemplateColumn Header="Timestamp">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Timestamp}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <!-- level -->
        <DataGridTemplateColumn Header="Level">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Level}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <!-- error message -->
        <DataGridTemplateColumn Header="Message">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Message}" …
Run Code Online (Sandbox Code Playgroud)

c# wpf datagrid .net-4.0 wpfdatagrid

4
推荐指数
1
解决办法
1676
查看次数

数组和列表与结构的差异

在以下代码中,从数组和列表中获取结构.通过索引获取项目时,数组似乎通过引用执行,而列表似乎按值执行.有人可以解释这背后的原因吗?

struct FloatPoint {
    public FloatPoint (float x, float y, float z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    public float x, y, z;
}

class Test {

    public static int Main (string[] args) {
        FloatPoint[] points1 = { new FloatPoint(1, 2, 3) };

        var points2 = new System.Collections.Generic.List<FloatPoint>();
        points2.Add(new FloatPoint(1, 2, 3));

        points1[0].x = 0; // not an error
        points2[0].x = 0; // compile error

        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

将结构定义更改为类可以进行编译.

c# struct list

4
推荐指数
1
解决办法
60
查看次数

在c#中使用结构数组

我需要有关结构初始化的数组的帮助.在下面的代码中,我们如何完成注释中定义的初始化?

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# struct

3
推荐指数
1
解决办法
300
查看次数

修改List <T>的内容时"无法修改返回值"错误

我目前正在开发一个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)

c#

3
推荐指数
1
解决办法
1206
查看次数

获取;设置;使用 DateTime 来验证 .TryParse?

我正在尝试将输入数据的验证移至 get;set; 中 类结构的。

public void PlotFiles()
    {

     List<DTVitem.item> dataitems;
        DTVitem.item i;
        DateTime.TryParse("2012/01/01", out i.dt);
        DateTime.TryParse("04:04:04", out i.t);
        int.TryParse("455", out i.v);
        dataitems.Add(i);

    }
Run Code Online (Sandbox Code Playgroud)

该结构在单独的类中声明(可能没有必要):

public partial class DTVitem
{
    public struct item
    {
        public DateTime dt;
        public DateTime t;
        public int v;
    }
}
Run Code Online (Sandbox Code Playgroud)

每次我设置DTVitem.item.dtDTVitem.item.t或 时DTVitem.item.v,我希望它执行相关操作.TryParse()来验证属性内容。

但是,当我尝试按如下方式使用 TryParse() 时(试图理解MSDN 中的这个示例):

public partial class DTVitem
{
    private DateTime _datevalue;

    public string dt
    {
        get { return _datevalue; }
        set { DateTime.TryParse(value, …
Run Code Online (Sandbox Code Playgroud)

c# get properties set

3
推荐指数
1
解决办法
2万
查看次数

Write fast包含list的方法,listitem是vector

我有给定的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)

c#

3
推荐指数
1
解决办法
254
查看次数

不能在匿名方法中使用ref或out参数

我在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)

c# xml lambda

3
推荐指数
2
解决办法
8376
查看次数

标签 统计

c# ×9

struct ×4

list ×2

.net ×1

.net-4.0 ×1

datagrid ×1

get ×1

lambda ×1

performance ×1

properties ×1

set ×1

wpf ×1

wpfdatagrid ×1

xml ×1