Understanding a for() statement in c# [closed] - c#

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'm going through a book at the moment (C# 8.0 and .NET Core 3.0) and I'm stuck with understanding / Googling the explanation for this question.
Does the following statement compile? for ( ; true; ) ;
I chucked the code in my editor and it does compile, but any code below is unreachable (which is fine). I'm hoping someone can explain what this statement is doing.
Cheers

In all variants of C a for is comprised of three parts: initialization, condition and update.
Initialization is executed once when the for is initialized, for example declaring or updating a variable: for(int a = 0;//...
Afther that comes the condition that is checked on each loop, for example: a < 10; //..
And finally we have the update, something that will be executed after each loop: a++)
It's not mandatory to set any of these, you can even do for( ; ; ) ;, that would create an infinite loop.
So, the code you have will create a for that will check if true is true creating an infinite loop.

Related

Dealing with multiple booleans [closed]

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 days ago.
This post was edited and submitted for review 2 days ago.
Improve this question
I have a Customer data object. That object has to be evaluated by different functions. Each function will return true or false.
Since each function evaluate different case scenarios, I need to submit the Customer object to each one of them.
The way the functions were written, only one of them is going to return true.
(The functions are used in another part of the code, so I am using them as it is, so I don't have to repeat code).
The WorkStatus is an Enum. So, if the function returns true, then the Enum will be set with the specified value.
So:
WorkStatus result = 0;
if (notStarted) result = WorkStatus.NotStarted;
if (started) result = WorkStatus.Started;
if (inProgress) result = WorkStatus.InProgress;
if (withBacklog) result = WorkStatus.WithBacklog;
if (finalized) result = WorkStatus.Finalized;
return (int)result;
I made this way, but I am sure there is some better way. I just can't figure this out.
Obs.: I wrote the question again. I hope it's more clear. Sorry for the previous lack of details.

Creating 100 variables [closed]

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.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
So I created an array with 100 variables using Enumerable.Range. The data type is limited to Int32.
Problem
How can I create the same array with SByte?
Am I right in thinking I would need to use a loop to create and index the variables?
I have looked around online and most results touch on declaring counting variables for the loop but not using a loop to declare variables
Just cast them:
SByte[] array = Enumerable.Range(0, 100).Select(i => (SByte) i).ToArray();
note that SByte is not cls compliant, you might want to use short instead.

C# 8.0 Using Declarations [closed]

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
In C# 8.0 we can now use using declarations in C# 8.0. Are they really such a good idea? Consider this using statement:
private int SomeMethod()
{
using (var t = new StreamWriter("somefile.txt"))
{
} // dispose of variable t
// 100 lines of code
}
As soon as the closing brace is reached, the variable t is disposed of. With using declarations, the scenario is different:
private int SomeMethod()
{
using var t = new StreamWriter("somefile.txt");
// 100 lines of code
} // dispose of variable t
The variable t is only disposed at the end of the method. Using statements seem more efficient to me, because you only keep the object "alive" for as long as you need it.
The answers can be as many as different scenarios.
In your case for example it could either be:
The function is big enough that it do would make sense to split. Remember that in modern programming with unit testing in mind, the units must be sufficiently small and the functions to do specific things.
The 100 lines will end in quite quickly. If that's the case, then it's ok to use the new more readable definition.
The same resources are needed a few lines below. Then why not use the same instance and then dispose?
In the rest of the lines, something else happens that takes time. Then it does not make sense to keep an item non-disposed (like a Stream) and the old way should be used.
The list could go on. There is no one solution fits all example but in most cases I think the first applies.

Do while loop check for last iteration [closed]

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 5 years ago.
Improve this question
This may sound like an elementary question, but I want to check whether my thought process is correct.
I have a do while loop and I want to check when the loop has entered the last iteration so as to either set a value or not. I have come with this sort solution but doesn't feel and I was wondering whether there is a another way.
do
{
int iterationCheck ++;
if( iterationCheck! = counter)
// Do something
counter++;
} while (true)
Is this correct or is there a better way?
Sure you could do this to count the iteration
But put the 'int iterationCheck=0' variable declaration before the do/while scope.
You could go for a 'for' loop too ( which would be better )
However it all depends on the condition of your loop.
Because we can't infer the 'out' condition of your loop so we can't provide a accurate answer.

What is it called when several conditional code blocks are inside each other? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I cant for the life of me remember what the word is. It's when you ???? several if/else/for/while/usings inside each other.
bool isTrue = true, isFalse = true, HasForgottenWord = true;
if( isTrue )
{
if( isFalse )
{
if( HasForgottenWord )
{
Console.WriteLine("Ask on StackOverflow.com - kthx bye");
}
}
}
It's called nesting
Its called "nesting" them :)
nested maybe what you're looking for.
I think nesting might be what he's looking for...
... right: It's called nesting and deep nesting is generally considered a code smell.
Loop block nesting can be reduced by the Linus-Torvalds-Trick (see Mono Coding Guidelines).
It is called as nesting loops

Categories