Home > Net >  C # generic function problem
C # generic function problem

Time:03-26

I have a List And a List
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 understand

CodePudding user response:

Private void UpdateHistoryALL (T Value, List HistoryList List TimeList)
{
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 operands

CodePudding user response:

HistoryLis. Last ()!=Value

CodePudding 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 ()
{
List BoolList=new List (a);
UpdateHistoryALL (boolList, true);

List Times=new List (a);
UpdateHistoryALL (times, DateTime. Now);

List Texts=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 ()
{
List BoolList=new List (a);
UpdateHistoryALL (boolList, true);

List Times=new List (a);
UpdateHistoryALL (times, DateTime. Now);

List Texts=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 above

CodePudding user response:

Private void UpdateStringListHistory (string Value, List HistoryList List TimeList)
{
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 HistoryList List TimeList)
{
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);
}
}
  •  Tags:  
  • C#
  • Related