FROM#. Is this a valid conditional statement?

I have two methods: A and B Only after the success of the two methods that I have to insert into the database. So, is this a valid conditional statement in C #?

 if (A() && B()) { //insert into db } 

After executing method A I need to execute B If both of them are successful, I have to do an insert.

+4
source share
6 answers

Yes this is true. But note that B will only execute if A() returns true.

+8
source

This is called a short circuit rating.

Conditions will be evaluated in order of appearance, and only if their testing is still relevant. That is, if A () fails, there is no reason to check B ().

+2
source

As they say, B () is not executed if A () returns false. If you want both functions to be executed, I recommend something like:

 bool resultA = A(); bool resultB = B(); if(resultA && resultB) { //insert into db } 
0
source

There have already been other answers to your question, but just to clarify, since there are several misleading posts here ...

The && operator is shorted

if (false && Foo()) // Foo() is not run

The & operator has no short circuit

if (false & Foo()) // Foo() is run

Use the latter if your features have side effects that you want to provide.

0
source

set fa = 1 fd = 1 inside both functions.

Then check

if (fa == FB == 1) {// do}

-2
source

It should be

 if(A()) { if(B()) { //insert into db } } 

This will ensure that both functions are performed.

-7
source

Source: https://habr.com/ru/post/1309549/


All Articles