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 callfunction()
gets the return
value of a function- A third way could be
() => function()
which
creates a new anonymous function which is callingfunction
(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&&
like1 <= 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 purpose | Value inequality | Negated pattern matching |
Can perform value inequality | Yes | Yes |
Can perform negated pattern matching | No | Yes |
Can invoke implicit operator on left-hand operand | Yes | No |
Can invoke implicit operator on right-hand operand(s) | Yes | Yes1 |
Is its own operator | Yes | No2 |
Overloadable | Yes | No |
Since | C# 1.0 | C# 9.03 |
Value-type null-comparison branch elision4 | Yes | No[Citation needed]5 |
Impossible comparisons | Error | Warning |
Left operand | Any expression | Any expression |
Right operand(s) | Any expression | Only constant expressions6 |
Syntax | <any-expr> != <any-expr> | <any-expr> is [not] <const-expr> [or|and <const-expr>]* and more |
Related Topics
In C# What Does 'Where T:Class' Mean
How to Implement Solid Principles into an Existing Project
Change the Value in App.Config File Dynamically
Companyname.Foo' Is a 'Namespace' But Is Used Like a 'Type'
How to Find the Fully Qualified Name of an Assembly
Creating Powerpoint Presentations Programmatically
Calling a Method in Parent Page from User Control
Find Methods That Have Custom Attribute Using Reflection
Register Background Task in Silverlight 8.1 App
Photo Capture on Windows Store App for Windows Phone
Cssrewriteurltransform with or Without Virtual Directory
Connect to Active Directory via Ldap
Dataannotations: Recursively Validating an Entire Object Graph