When the data in the List is tb_SensorRecordModel) type (class, reference data, can modify the values in the List;
When the data in the List is Int (a value of type integer data), can not modify the values in the List; Problem is how to change?
The namespace ConsoleApp9
{
Public class tb_SensorRecordModel
{
Public int ID {get; set; }
Public decimal Value1 {get; set; }
}
Class Program
{
The static void Main (string [] args)
{
Console. WriteLine (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- tb_SensorRecordModelList -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");
ListTb_SensorRecordModelList. Add (new tb_SensorRecordModel {ID=1, Value1=1});
Tb_SensorRecordModelList. Add (new tb_SensorRecordModel {ID=2, Value1=67});
Tb_SensorRecordModelList. Add (new tb_SensorRecordModel {ID=3, Value1=3});
//change the list of values of an element
Tb_SensorRecordModel model1=tb_SensorRecordModelList. Where (=c & gt; C.I D==2). FirstOrDefault ();
Console. WriteLine (" model1 original values for "+ model1. Value1);
Model1. Value1 + +;
Console. WriteLine (" model1 values increase after 1 for "+ model1. Value1);
Tb_SensorRecordModel model2=tb_SensorRecordModelList. Where (=c & gt; C.I D==2). FirstOrDefault ();
Console. WriteLine (" after step 1, tb_SensorRecordModelList value instead of "+ model2. Value1);
Console. WriteLine (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- tb_SensorRecordModelList -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");
ListIntList=new List () {65, 66, 67, 68, 69};
Int findInt=67;
Int IntTest1=IntList. Where (x=& gt; X.equals (findInt)). The First ();
Console. WriteLine (" IntTest original values for "+ IntTest1);
IntTest1 + +;
Console. WriteLine (" IntTest values increase 1 for "+ IntTest1);
Int IntTest2=IntList. Where (x=& gt; X.equals (findInt)). The First ();
Console. WriteLine (" after step 1, ntTestList value instead of "+ IntTest2);
Console.Read();
}
}
}
CodePudding user response:
Can't, can only use a class packageCodePudding user response:
Can modify the values in the List, however,To modify the List,
Rather than to modify an element of copy ,
static void Main (string [] args)
{
ListIntList=new List () {65, 66, 67, 68, 69};
Int findInt=67;
Var index=IntList. FindIndex (x=& gt; X==findInt);//find the location of the target under the IntList,
Console. WriteLine (" IntTest original values for "+ IntList [index]);
IntList [index] + +;
Console. WriteLine (" IntTest values increase after 1 for "+ IntList [index]);
Console.Read();
}
CodePudding user response:
This is a typical value types and reference types you don't understand,int IntTest1=IntList. Where (x=& gt; X.equals (findInt)). The First ();
Console. WriteLine (" IntTest original values for "+ IntTest1);
IntTest1 + +;
Change
IntList. Where (x=& gt; X.equals (findInt)). The First () + +;