Recursion, and the difference between -- and i-1 [duplicate] - c#

This question already has answers here:
pre Decrement vs. post Decrement
(2 answers)
Closed 6 years ago.
I was doing some C# practice and decided to make a basic function to sum the contents of an integer array.
Originally I wrote my code as follows:
if(index == 0)
return toSum[index];
else
return toSum[index] + sum(toSum, index--);
Now that code resulted in a StackOverFlow exception. This made no sense to me; surely this is how one would do a summation? Turns out the problem was in the index--. When I changed it to index - 1 it worked out fine, thus I was wondering why is that the case? My understanding is that it is simply a shorthand for index = index-1. I was wondering if anyone could explain the reason behind this behavior.

Post-decrement operator returns the value before decrementing, so in your case the index will never be 0 and the function won't stop calling itself and you'll get a stack overflow. You want to write --index instead. It will return the value after decrementing then.

Related

C# short if statement to execute a function if true [duplicate]

This question already has answers here:
Shortest way to check for null and assign another value if not
(12 answers)
Closed 2 years ago.
Is it possible to execute a function within a short statement if the statement is true?
Something like that:
myObject.subObject != null ?? Db.LoadReferences(myObject.subObject); // ORMLite function
yes:
if(myObject.subObject != null) Db.LoadReferences(myObject.subObject);
At just 1 single character more than your original.

How to do a count on an IEnumerable? [duplicate]

This question already has answers here:
The best way to get a count of IEnumerable<T>
(11 answers)
Closed 2 years ago.
I want to be able to test a count of obj below but not able to do so.
when I mouse over obj it shows a count of 3
<IEnumerable<File> obj = await _mytester.cleanup(myitems)
why cant i do this?
if(obj.count > 0)
The liine above is giving me an error
You can't do that because count doesn't exist on IEnumerable and that's by design.
You can use IEnumerable.Count() like this:
if (obj.Count() > 0)
But that's horribly inefficient since you're really just checking if there's anything there.
You're better off doing this:
if (obj.Any())
Both of these are provided by LINQ extension methods.
It's very important to note that Count() is iterating through all elements and is nowhere near as efficient as something like ICollection.Count

Given two arrays, how can I tell if a math result would overflow? [duplicate]

This question already has answers here:
Best way to handle Integer overflow in C#?
(6 answers)
Closed 5 years ago.
In this case, sum is an int and I am intentionally setting one[] and [two] to int.MaxValue.
The result for sum is 1.
How can I tell if the result of a math function is going to overflow here, prior to attempting it?
sum += one[i] * two[i];
By 'overflow', did you mean it will exceed int.MaxValue?
If so, try to use checked block.
As #AlexD mentioned in comments, it won't perform a prior-check; however, it will raise an exception in case of overflow.
Quote and code sample are taken from MSDN:
The checked keyword is used to explicitly enable overflow checking for integral-type arithmetic operations and conversions.
// If the previous sum is attempted in a checked environment, an
// OverflowException error is raised.
// Checked expression.
Console.WriteLine(checked(2147483647 + ten));
// Checked block.
checked
{
int i3 = 2147483647 + ten;
Console.WriteLine(i3);
}

Can't cast 0 or 1 to a boolean in C# [duplicate]

This question already has answers here:
Can't cast int to bool
(11 answers)
Closed 9 years ago.
I was playing around with type casting earlier today, and came across a something interesting. The C# compiler is unable to cast 0 or 1 to a boolean data type. For example:
bool b = (bool)0;
would return false (if it was able to compile).
There doesn't seem to be any loss of information here, so my question is, is there some specific reason the C# compiler doesn't let you do this?
Because in order for your cast to work, every int would have to be able to be cast to bool, not just 1 and 0. The decision to not allow all integers to be treated as boolean values was done purposefully, to remove the possibility for the kinds of errors this allowance accounts for in languages where it is allowed, e.g. C and C++.

strange for loop? C# [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C# is a for(;;) safe and what does it really do?
So i recently came across something ive never seen before..
for (; ; )
{
}
What is exactly happening when the feilds are left blank like that?
It's an infinite loop.
Somewhere inside there should be a break; statement, or possibly an exception thrown in order for control to pass beyond the loop.
You could also achieve the same thing (probably more obviously) by doing
while (true)
{
// do stuff
}
This is an infinite loop, almost equivalent to a while(true) loop.
The break condition is not there in between the two semicolons, therefore, it must be there somewhere in the loop body.
That's an infinite for loop.

Categories

Resources