Each variable has a scope . When a variable in c is defined in a block (which means between { and a } ), it is limited to this area, that is, it can only be referenced in this area. Therefore, in the last line, you are outside the scope of c1 , c2 , c3 and cannot refer to them.
You need to define them outside the block:
double c1, c2, c3; if(API>30) { c1 = 0.0178; c2 = 1.187; c3 = 23.931; } else { c1 = 0.0362; c2 = 1.0937; c3 = 25.7240; } double Rs2 = (c1 * sg * Math.Pow(p, c2)) * Math.Exp(c3 * (API / T));
source share