Method is to check the List of the last element is equal to the value of the current incoming, if the range, the value behind the add to the List
At the same time, to judge whether the list is empty, if it is empty, can directly add,
But the string may be "", so more don't know how to write
T want to use to write, but stuck,
Please write
CodePudding user response:
No description is clear, look not to understandCodePudding user response:
Private void UpdateHistoryALL{
Int Count=HistoryList. Count;
If (typeof (T)==typeof (bool))
{
If (Count==0)
{
HistoryList. Add (Value);
TimeList. Add (DateTime. Now);
}
The else
{
}
}
}
Wrote this step can't write down the if (the Value!=HistoryList] [Count - 1) this is an error, how to write better
The value and the list of the last value to compare
CodePudding user response:
Operator "!="cannot be applied to" T "and" T "type of the operandsCodePudding user response:
HistoryLis. Last ()!=ValueCodePudding user response:
public void UpdateHistoryALL(List The list, T value)
{
If (list. Count==0)
{
List. The Add (value);
}
The else
{
List. The Clear ();
}
}
Private void Test ()
{
ListBoolList=new List (a);
UpdateHistoryALL(boolList, true);
ListTimes=new List (a);
UpdateHistoryALL(times, DateTime. Now);
ListTexts=new List (a);
UpdateHistoryALL(Text, "Text");
}
CodePudding user response:
public void UpdateHistoryALL(List The list, T value)
{
If (list. Count==0 | |! List. Last(.) the Equals (value))
{
List. The Add (value);
}
The else
{
}
}
Private void Test ()
{
ListBoolList=new List (a);
UpdateHistoryALL(boolList, true);
ListTimes=new List (a);
UpdateHistoryALL(times, DateTime. Now);
ListTexts=new List (a);
UpdateHistoryALL(the texts, "text");
}
If you want to determine whether the last equal, use this
CodePudding user response:
private void UpdateHistoryALL(T Value, List HistoryList List TimeList)
{
//handle empty collection
HistoryList=HistoryList?? New List(a);
//add value does not allow null
If (the Value!=null & amp; & (
//if the string is not empty string
(typeof (T)==typeof (string) & amp; & ! String. IsNullOrEmpty (Value. The ToString ()))
| |
(typeof (T)!=typeof (string))
)
//set is empty or set the last item!=when the value added to the collection of
& & (HistoryList. Last ()==null | |! HistoryList. Last (.) the Equals (Value)))
{
HistoryList. Add (Value);
}
}
CodePudding user response:
As an error, the information as aboveCodePudding user response:
Private void UpdateStringListHistory (string Value, List{
Int Count=HistoryList. Count;
If (Count==0)
{
If (the Value!="")
{
HistoryList. Add (Value);
TimeList. Add (DateTime. Now);
}
}
Else if (the Value!=HistoryList. Last ())
{
HistoryList. Add (Value);
TimeList. Add (DateTime. Now);
}
}
Private void UpdateBoolListHistory (bool Value, List
{
Int Count=HistoryList. Count;
If (Count==0)
{
HistoryList. Add (Value);
TimeList. Add (DateTime. Now);
}
Else if (the Value!=HistoryList. Last ())
{
HistoryList. Add (Value);
TimeList. Add (DateTime. Now);
}
}
These two basic function is the same, is there a way to use a generic done?
CodePudding user response:
Private void UpdateHistoryALL(T Value, List HistoryList)
{
if (! HistoryList. Any ())
{
HistoryList. Add (Value);
return;
}
Var lastValue=(https://bbs.csdn.net/topics/HistoryList.LastOrDefault);
//use the Equals judgment
if (! LastValue. Equals (Value)) HistoryList. Add (Value);
}
CodePudding user response:
CodePudding user response:
private void UpdateBoolListHistory(T Value, List The list, List Time_ist)
{
Int Count=list. Count;
If (Count==0)
{
List. The Add (Value);
Time_ist. Add (DateTime. Now);
}
Else if (! Value. The Equals (list. The Last ()))
{
List. The Add (Value);
Time_ist. Add (DateTime. Now);
}
}