Is There a "Between" Function in C#

Is there a between function in C#?

No, but you can write your own:

public static bool Between(this int num, int lower, int upper, bool inclusive = false)
{
return inclusive
? lower <= num && num <= upper
: lower < num && num < upper;
}

What's the difference between function and function() in C#?

  • function refers the address of a function you can call
  • function() gets the return
    value of a function
  • A third way could be () => function() which
    creates a new anonymous function which is calling function (a simple wrapper in this case)

Since function() returns no other function

Thread t = new Thread(function()); 

would not work because a thread needs a function to call. This means you have to use

Thread t = new Thread(function); 

or

Thread t = new Thread(() => function());

to pass a function to a thread.

Difference between Method and Function?

Both are same, there is no difference its just a different term for the same thing in C#.

Method:

In object-oriented programming, a method is a subroutine (or procedure
or function) associated with a class.

With respect to Object Oriented programming the term "Method" is used, not functions.

what's the difference between function calls?

Both methods accept an IA, but the generic one has knowledge of the real object being passed.

Consider what would happen if you return obj from your function:

public static IA Test1(IA obj)
{
return obj;
}

public static T Test2<T>(T obj) where T : IA
{
return obj
}

The non generic can only ever return an IA interface, because that's all it gets.

The generic on the other hand can return a concrete object, and effectively becomes a different method for every different type of object that it gets called with:

static void Main(string[] args)
{
var a = new A();
var b = new B(); //also implements IA

IA ret1 = Test1(a); // this can only return an IA
A ret2A = Test2(a); // this returns an A
B ret2B = Test2(b); // this returns a B! It's effectively a different function
}

How to elegantly check if a number is within a range?

There are a lot of options:

int x = 30;
if (Enumerable.Range(1,100).Contains(x)) //true

And indeed basic if more elegantly can be written with reversing order in the first check:

if (1 <= x && x <= 100)   //true

Also, check out this SO post for regex options.

Notes:

  • LINQ solution is strictly for style points - since Contains iterates over all items its complexity is O(range_size) and not O(1) normally expected from a range check.

    More generic version for other ranges (notice that second argument is count, not end):

    if (Enumerable.Range(start, end - start + 1).Contains(x)
  • There is temptation to write if solution without && like 1 <= x <= 100 - that look really elegant, but in C# leads to a syntax error "Operator '<=' cannot be applied to operands of type 'bool' and 'int'"

Is there BETWEEN DateTime in C# just like SQL does?

There is not a Between function but should be easy enough to add one

public static bool Between(DateTime input, DateTime date1, DateTime date2)
{
return (input > date1 && input < date2);
}

It is possible to define something like 'between' in if-else statement in C#?

Define an extension method:

public static bool Between(this int source, int a, int b)
{
return source > a && source < b;
}

Then, use it:

if (val.Between(20, 40))
//...

As oɔɯǝɹ correctly pointed out in his comment, you could go one step further and support all implementers of IComparable<T> with a generic extension method:

public static bool Between<T>(this T source, T a, T b) where T : IComparable<T>
{
return source.CompareTo(a) > 0 && source.CompareTo(b) < 0;
}

Is there a difference between != and is not in C#?

Comparison table:













































































Operator!=is not
Original purposeValue inequalityNegated pattern matching
Can perform value inequalityYesYes
Can perform negated pattern matchingNoYes
Can invoke implicit operator on left-hand operandYesNo
Can invoke implicit operator on right-hand operand(s)YesYes1
Is its own operatorYesNo2
OverloadableYesNo
SinceC# 1.0C# 9.03
Value-type null-comparison branch elision4YesNo[Citation needed]5
Impossible comparisonsErrorWarning
Left operandAny expressionAny expression
Right operand(s)Any expressionOnly constant expressions6
Syntax<any-expr> != <any-expr><any-expr> is [not] <const-expr> [or|and <const-expr>]*
and more



Related Topics



Leave a reply



Submit