c# - Why use a single variable as a conditional evaluation? -
c# - Why use a single variable as a conditional evaluation? -
in way of understanding conditionals throughout programming, whether if statement, while loop, etc - particular statement checked either true of false.
this how utilize conditionals, come across other people's code containing unusual conditionals there doesn't seem particular evaluation, ordinary variable. example:
sometype avariable = somevalue; if (avariable) { //do }
what evaluating true, or false, in case? if avariable represents bool, straightforward, explicit value isn't bool @ all, , contains null. 1 time again, i'm seeing in every language i've looked @ assuming not language specific.
edit 1: i've been made aware may dependent on language, add together see in c#, , variable in question can either reference type or value type.
edit 2: answers, have summarized below:
(a) language specific whether or not particular non-bool variable may evaluated bool (b) in many cases, if language supports such evaluation, non-zero, non-empty, or non-null value typically evaluates true (c/c++ int's) (c) languages don't back upwards such implicit evaluation, cast bool needs added in class definition, illustration (c#)
as other answers have pointed out, details language-specific. in c# can if avariable
bool
or has implicit conversion bool
(or implements true
, false
operators).
in unity
link, appears base of operations object
type has implicit conversion bool returns whether object null e.g.
public class object { public static implicit operator bool(object b) { homecoming b != null; } }
c# variables conditional
Comments
Post a Comment