Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Sometimes i need to use != two, three or four times is there a better way to do it?
if (result != 1 && result != 12)
{
do something
}
if (result != 1 && != 12)
{
do something
}
second option would be better, nut we all know it doesn't work
There is not a direct way in the language like the one you suggested:
// Wont compile
if (result != 1 && != 12) { }
If you only have a few values to check, I'd use the explicit comparsions. However you can create a collection of values an check if the result is not in the colleciton:
// using System.Linq;
if (!(new []{ 1, 2 /*, ... */ }).Contains(result)) { }
As suggested in the comments you can also write an extension method. This requires a public static class:
public static class ExtensionMethods
{
public static bool NotIn(this int num, params int[] numbers)
{
return !numbers.Contains(num);
}
}
// usage
result.NotIn(1, 12);
result.NotIn(1, 12, 3, 5, 6);
And if you want to compare not only integers, you can write a generic method:
public static bool NotIn<T>(this T element, params T[] collection)
{
return !collection.Contains(element);
}
// Works with different types
result.NotIn(1, 2, 3, 4);
"a".NotIn("b", "c", "aa");
You can use Linq All() method probably
if((new int[]{1,12,13}).All(x => x != result))
{
// do something
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
What's the easiest way to check if after an index there is only empty content.
string[] tab = {"a","b","c","","",""}
for example Check(tab[2]) will be true Check(tab[1]) will be false.
can I do it with lambda expression tab.XXX without a loop ?
I assume you want a simple and readable way.
You can use Skip and All (note: not using the index but item-count):
bool emptyAfter = tab.Skip(2).All(x => string.IsNullOrEmpty(x));
But you could also put it into this extension method to reuse it with any type:
public static class EnumerableExtensions
{
public static bool IsEmptyAfter<T>(this IEnumerable<T> seq, int skipItems, Func<T, bool> isEmpty = null)
{
if(isEmpty == null) isEmpty = x => EqualityComparer<T>.Default.Equals(x, default(T));
return seq.Skip(skipItems).All(x => isEmpty(x));
}
}
bool emptyAfter = tab.IsEmptyAfter(2, string.IsNullOrEmpty);
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I was wondering if anyone could please tell me a nicer way of writing this. These are the only conditions that are used.
private static void BondedNonBoundedIndicator(InvoiceLine invoiceLine, Product packageProduct)
{
var value = packageProduct.BondedQuantity;
var value2 = packageProduct.NonBondedQuantity;
if (value == 0 && value2 == 1)
{
invoiceLine.BondedORBbondedIndicator = "N";
}
if (value == 1 && value2 == 0)
{
invoiceLine.BondedORBbondedIndicator = "Y";
}
}
Using C# 8, we can use tuples and the switch expression to simplify this:
invoiceLine.BondedORBbondedIndicator = (value, value2) switch {
(0, 1) => "N",
(1, 0) => "Y",
_ => invoiceLine.BondedORBbondedIndicator
};
You could shorten the last case even further if you know the invoiceLine.BondedORBbondedIndicator before the code is run is always, say ""(, then the last case would be _ => "").
You can use xor logic
If(value1^value2 == 1)
value1 == 0 ? invoiceLine.BondedORBbondedIndicator = "N" : invoiceLine.BondedORBbondedIndicator = "Y";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
public class hamid
{
private int[] arr = new int[10];
public int[] Arr
{
get => arr;
set
{
if (value < 0)
Environment.Exit(1);
else arr = value;
}
} // If in error.
}
I want have a if statement. For example if (values < 0)
But I have an error, please help me.
value is an array of integer, it can't be zero. Any element in the array could be zero. If you are trying to determine if the array is null, then you could change the check to be:
if (value == null)
{
Environment.Exit(1);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In case of situations to use multiple params type parameters in methods what is the alternate because i get error while trying to use multiple params type parameters
Use plain arrays instead:
public void Method( /*remove params*/ string[] first
, /*optionally remove params*/ int[] second
)
{ }
Call it:
Method(new string[] { ... }, new int[] { ... } );
Optionally, you could leave the last params as is, since it can only be one per method and must be the last one, according to MSDN:
No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.
For simplistic use, I'd go with the solution Patrick Hofman supplied. Although, whenever you want to do something more type specific and including a wider range of types, I'd say you should be using params.
public class Bar { }
public void Foo(params object[] objs)
{
foreach(object obj in objs)
{
Type typeofObject = obj.GetType();
if (typeofObject == typeof(string))
{
// Its a string
}
else if (typeofObject == typeof(int))
{
// Its an integer
}
else if (typeofObject == typeof(Bar))
{
// Its an Bar object
}
}
}
Calling the method:
Bar bar = new Bar();
this.Foo("hi", 1, bar);
But again, this isn't the right solution if you only have 2/3 types.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how do i allow this get to be null?
public double? GetSignalAverage
{
get
{
var signalaverage = Gsmdata.Select(x => x.SignalStrength)
.Average();
return Math.Round(signalaverage, 2);
}
}
SignalStrength is an Int
Check if the collection is null or empty:
public double? GetSignalAverage
{
get
{
if(Gsmdata == null || GsmData.Count() == 0)
return null;
var signalaverage = Gsmdata.Select(x => x.SignalStrength)
.Average();
return Math.Round(signalaverage, 2);
}
}
You'll have to declare SignalStrength as int? to allow null values. (If you don't, int will be its default value, which is 0.) But even then, Average() will simply ignore those values, if there is any non-null element (see the documentation).
If they all are null, Average will return null, which you can catch and return, like this:
double? signalaverage = Gsmdata.Select(x => x.SignalStrength)
.Average();
if(signalaverage == null)
{
return signalaverage; // which is essentially 'return null;'
}
else
{
return Math.Round(signalaverage, 2);
}