相关疑难解决方法(0)

C#按值传递而不是按引用传递

请考虑以下代码(我有意将 MyPoint编写为此示例的引用类型)

public class MyPoint
{
    public int x;
    public int y;
}
Run Code Online (Sandbox Code Playgroud)

它是普遍公认的(至少在C#中)当你通过引用传递时,该方法包含对被操作对象的引用,而当你通过值传递时,该方法复制被操纵的值,因此全局范围中的值是不受影响.

例:

void Replace<T>(T a, T b)
{
    a = b;
}

int a = 1;
int b = 2;

Replace<int>(a, b);

// a and b remain unaffected in global scope since a and b are value types.
Run Code Online (Sandbox Code Playgroud)

这是我的问题; MyPoint是引用类型,所以我希望在相同的操作Point,以取代ab在全球范围内.

例:

MyPoint a = new MyPoint { x = 1, y = 2 };
MyPoint b = new …
Run Code Online (Sandbox Code Playgroud)

c# value-type reference-type pass-by-reference pass-by-value

9
推荐指数
2
解决办法
7759
查看次数

列出所有位置的覆盖数据

我创建了一个包含3个字段的类:

class Gente
    {
        int _ID;
        string _nome, _sexo;


        public Gente()
        {
            _ID = 0;
            _nome = "";
            _sexo = "";
        }

        public int ID
        {
            set { _ID = value; }
            get { return _ID; }
        }

        public string Nome
        {
            set { _nome = value; }
            get { return _nome; }
        }

        public string Sexo
        {
            set { _sexo = value; }
            get { return _sexo; }
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后我从该类声明了一个List,并从该类声明了一个对象,以便我能够添加到List中.

List<Gente> Alunos = new List<Gente>();        

        Gente professor = …
Run Code Online (Sandbox Code Playgroud)

c# list

6
推荐指数
1
解决办法
4335
查看次数

IList.Add()覆盖现有数据

我正面临着向a添加数据IList的问题,但问题是每次添加数据时现有数据都被当前数据覆盖,我的代码如下:

Test test = new Test();
IList<Test> myList = new List<Test>();

foreach (DataRow dataRow in dataTable.Rows)
{
     test.PatientID = Convert.ToInt64(dataRow.ItemArray[0]);
     test.LastName = dataRow.ItemArray[1].ToString();
     test.FirstName = dataRow.ItemArray[2].ToString();
     myList.Add(test);
}
Run Code Online (Sandbox Code Playgroud)

这背后的原因是什么?

c# ilist

5
推荐指数
1
解决办法
8809
查看次数

List.Add()的问题仅保存最后添加的项目

我注意到的问题是这行代码:

tempList.Add(orderables);
Run Code Online (Sandbox Code Playgroud)

在这个完整的代码中:

AssociatedComboItems ai = new AssociatedComboItems();
List<Orderables> tempList = new List<Orderables>();
Orderables orderables = new Orderables();

foreach (var t in comboBox1.Items)
{
    ai.ComboBoxItem = t.ToString();

    for (int i = 0; i < fpSpread1.ActiveSheet.RowCount; i++)
    {
        orderables.Display = fpSpread1.ActiveSheet.Cells[i, 1].Text;
        orderables.ShowInDSR = (bool)fpSpread1.ActiveSheet.Cells[i, 0].Value;
        orderables.DisplayOrder = i;
        tempList.Add(orderables);
    }

    ai.AssociatedItems = tempList;
    tempList.Clear();
    if(AssociatedItems == null)
    AssociatedItems = new List<AssociatedComboItems>();
    AssociatedItems.Add(ai);
}
Run Code Online (Sandbox Code Playgroud)

当我将断点放在上面提到的那一行(tempList.Add(orderables);)时,它第一次正确地将项添加到templist它,它将有一个项目.第二次它会将正确的项目添加到列表中但是如果我将鼠标悬停在tempList想要查看其内容的位置,虽然它有两个项目,但它们都是相同的 - 它们现在都是添加到列表中的第二个项目.它覆盖了第一个.

我无法弄清楚这有什么问题,为什么会发生这种情况.

c#

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

c#list覆盖问题

我有一些代码如下所示,它将a的值写入gridview列表,如图所示.代码确实获得了正确的值,但是当网格的第二行添加到列表时,它会覆盖列表的第一行.
谁知道为什么会这样?

C#代码

 List<Item> lstNewItems = new List<Item>(); // Control Items  
 lstNewItems.Clear();
 Item NewItem = new Item();
 foreach (GridViewRow PendingItemUnderControl in GridViewPendingList.Rows)
 {
    NewItem.Paramater = PendingItemUnderControl.Cells[0].Text.ToLower();
    NewItem.Type = (String)Session["BrowseType"];
    lstNewItems.Add(NewItem);
 }
Run Code Online (Sandbox Code Playgroud)

c# gridview list

0
推荐指数
1
解决办法
4975
查看次数

为什么在字典<>中添加新值会覆盖其中的先前值

我一直在使用C#中的Dictionary进行此问题.每当我添加一个条目时,字典中的所有条目都填充相同的值.这是代码:

using System;
using System.Collections.Generic;

namespace TESTING
{
    class Program
    {
        /*--------------------Variable Declaration------------------------*/
        public static int NumberOfPlayers = 5;
        public static Dictionary<int, bool> PlayerAI = new Dictionary<int, bool>();
        public static Dictionary<int, Dictionary<string, int>> Players = new Dictionary<int, Dictionary<string, int>>();

        /*----------------------------------------------------------------*/
        public static void Main(string[] args)
        {
            Dictionary<string, int> TMP = new Dictionary<string, int>();
            for (int i = 0; i < NumberOfPlayers; i++)
            {
                Console.WriteLine(i);
                TMP.Clear();
                TMP.Add("Player Target", 0 + (i * 3));
                TMP.Add("Player Cash", 0 + (i * 3));
                TMP.Add("Player …
Run Code Online (Sandbox Code Playgroud)

c# dictionary

0
推荐指数
1
解决办法
348
查看次数

列表<>用旧列表覆盖值

我有这个代码:

List<List<string>> strs = new List<List<string>>();
List<string> str = new List<string>();

foreach (DataGridViewRow sr in dataGridView1.SelectedRows)
{
    if(i >= 10)
    {
        strs.Add(str); //Here strs have one str with 10 values
        str.Clear();
        i = 0;
        var a = strs; //Here strs have one str with 0 values
    }
    str.Add(sr.Cells["MOBILNI"].Value.ToString().Trim());
    i++;
}
strs.Add(str);
Run Code Online (Sandbox Code Playgroud)

我列出了strings我填充的列表,当它达到10个成员时,我将整个列表放入列表中List<string>,然后清除列表strings以填充下一个10项.问题是,当strings达到10个成员的列表时,我将其添加到列表中List<string>并在调试中我看到我strs有1个成员(List<string>)其中有10个元素(10个字符串)但是当我清除strings它的基本列表时它也清除了里面的列表List<List<string>>.

当我使用str = new List<string>()而不是str.Clear()它正常工作.

那为什么会发生这种情况以及如何克服它?

c# list

0
推荐指数
1
解决办法
86
查看次数

All items in list get the same value

I'm trying to create a list and insert values in to it. But when I add a new item it changes all existing items in the list to the same value I add and I don't understand why.

Here is my code:

public List<Times> CreateListOfTimes()
{
    var listOfTimes = new List<Times>();
    var times = new Times();
    int startTime = 8;
    int endTime = startTime + 1;
    for (int i = 0; i < 8; i++)
    {
        times.StartTime = startTime;
        times.EndTime …
Run Code Online (Sandbox Code Playgroud)

c#

-1
推荐指数
1
解决办法
67
查看次数