Contains function for C# (Linked list) comparing issue [duplicate] - c#

This question already has answers here:
C# difference between == and Equals()
(20 answers)
Are string.Equals() and == operator really same? [duplicate]
(8 answers)
Closed 5 years ago.
I'm writing a simple contains function which accepts a value and checks if it exists in the linked list. Returns a bool. However, when temp.Data is the same as data, as checked through debugging, it still proceeds to enter the while statement and cycle through the list, as if they are not equal. I have found a solution, by using toString() on all values. So I have to compare temp.Data.ToString() and data.ToString(). Then it works...I am new to C#, I haven't used the Object keyword much. I'm wondering if it's something to do with that? So my function works with my quick fix but I'm trying to understand why...thanks!
public bool Contains(Object data)
{
Node temp = head;
while((temp.Data != data) && (temp.Next != null))
{
temp = temp.Next;
}
//Rest of code

Related

Is it possible to add methods into variables [duplicate]

This question already has answers here:
Easiest way to extend a Struct (PointF)
(3 answers)
Extension methods versus inheritance
(9 answers)
Extend an existing struct in C# to add operators
(7 answers)
A way to extend existing class without creating new class in c#
(3 answers)
Closed 1 year ago.
Is it possible to add methods into variables, example being.
public static int BoolToInt(bool entry)
{
if(entry == true) { return 1; }
else { return 0; }
}
bool example = checkbox1.Checked;
RandomMethod(example.BoolToInt());
Looking into minimizing visual clutter and help readability (I find RandomMethod(example.BoolToInt(), example2.BoolToInt()); easier to read than RandomMethod(BoolToInt(example), BoolToInt(example2));) I was wondering if this was possible, upon research I found this Can you assign a function to a variable in C#? which feels like it's the right direction, but it makes the variable become the method, when I want to just add into it. I'm a newbie so I couldn't go from there to what I want nor know if it's theres a way to do it, also couldn't find much reading the Microsoft Docs.
You can use C# extension methods to "extend" exists types by declaring new methods for the extended type.
static class BoolExtensions {
public static int ToInt(this bool value) {
return value ? 1 : 0;
}
}
Then you can use as:
var example = true;
var exampleAsInt = example.ToInt();
Reference:
Extension methods

How can i validate if List<int> is empty? [duplicate]

This question already has answers here:
Check if list is empty in C#
(8 answers)
Closed 3 years ago.
This is my Code:
public int PostCanal(List<int> listchannel) {
if (listchannel == null || listchannel.Contains(0))
{
listchannel.Add(1);
}
This list has values coming from a checkbox menu. So, if the user uncheck all the options I want to still using "1" as default value.
On this case, the listchannel List<int> is arriving with [0] value. However the if condition is skipped.
Any idea? Also tried .Equals Method.
You could use Any Or Count to check if the list is empty or not, like the following code :
if(listchannel == null || !listchannel.Any())
{
}
//Or
if(listchannel == null || listchannel.Count == 0)
{
}
I hope you find this helpful.

Refactor method (C#) [duplicate]

This question already has answers here:
Simply check for multiple statements C#
(2 answers)
C# - Prettier way to compare one value against multiple values in a single line of code [duplicate]
(2 answers)
Closed 3 years ago.
I have got method:
private bool MyMethod(PlantType plantType)
{
return plantType.PlantMoveType == PlantMoveType.PlantReady
|| plantType.PlantMoveType == PlantMoveType.PlantRelase
}
Can I write it into other way? Maybe with LINQ?
One way is to put the enum values that you want to check against into an array, and call Contains.
return new[] { PlantMoveType.PlantReady, PlantMoveType.PlantRelase }
.Contains(plantType.PlantMoveType);
If you are using C# 7 or later, you can also write the method as expression-bodied:
private bool MyMethod(PlantType plantType) =>
new[] { PlantMoveType.PlantReady, PlantMoveType.PlantRelase }
.Contains(plantType.PlantMoveType);
Well a small simplification would be to pass the type (enum?) of the property PlantMoveType instead of PlantType as the parameter.
Beyond that, you could declare the types to check for as e.g. an array. In case you'd like to reuse that array, you can also declare it outside the scope of the method:
private static PlantMoveType[] _plantStates =
new []{PlantMoveType.PlantReady, PlantMoveType.PlantRelase};
private bool MyMethod(PlantMoveType plantMoveType)
{
return _plantStates.Contains(plantMoveType);
}

Finding if UpperCase of a value exists in a list of strings [duplicate]

This question already has answers here:
How to ignore the case sensitivity in List<string>
(12 answers)
Closed 7 years ago.
I have something like this:
string configKeys = "othewr|RDX|MDX";
and wrote a code like this to see if value "OTHER" exists in that list
List<string> values = configKeys.Split('|').ToList();
var b = values.Find(item => item.Trim().ToUpper() == "OTHER").FirstOrDefault();
but for example because I have typed it wrong "othewr" so it crashes, but just want it to tell me if it exists or not as a boolean. How can I change the code to do that and not crash?
Use Any. If the predicate evaluates for at least one value in the collection it returns true, else false:
var b = values.Any(item => item.Trim().ToUpper() == "OTHER");

A Shortcut for c# null and Any() checks [duplicate]

This question already has answers here:
How to check if IEnumerable is null or empty?
(23 answers)
Closed 7 years ago.
Often in C# I have to do this
if(x.Items!=null && x.Items.Any())
{ .... }
Is there a short cut on a collection ?
In C# 6, you'll be able to write:
if (x.Items?.Any() == true)
Before that, you could always write your own extensions method:
public static bool NotNullOrEmpty<T>(this IEnumerable<T> source)
{
return source != null && source.Any();
}
Then just use:
if (x.NotNullOrEmpty())
Change the name to suit your tastes, e.g. NullSafeAny might be more to your liking - but I'd definitely make it clear in the name that it's a valid call even if x is null.
I also do a check on items of the list to assure the list not just contains all null objects; so as enhancement to Jon Skeet answer:
public static bool NotNullOrEmpty<T>(this IEnumerable<T> source)
{
return source != null && !source.All(x => x == null);
}

Categories