Suppress "Use unrecognized local variable" error?

I have a way like this:

public static long? FromIpv4ToLong(this string ipAddress) { var octets = ipAddress.Split(IpSplitChar); if (octets.Length != 4) return null; var success = long.TryParse(octets[0], out long a) && long.TryParse(octets[1], out long b) && long.TryParse(octets[2], out long c) && long.TryParse(octets[3], out long d); if (!success) return null; return ((16777216L * a) + (65536L * b) + (256L * c) + d); } 

... now the variables a , b , c and d will never be "unassigned" by the point to which they will refer, but the compiler does not see that path. Is there a way to make the compiler just “build it anyway”? It seems silly to initialize these values ​​ahead of time.

+5
source share
3 answers

Just reorganize the code to avoid a pointless local variable that confuses things using the fact that inside the if body, the compiler knows that everything is definitely assigned:

 public static long? FromIpv4ToLong(this string ipAddress) { var octets = ipAddress.Split(IpSplitChar); if (octets.Length != 4) return null; if (long.TryParse(octets[0], out long a) && long.TryParse(octets[1], out long b) && long.TryParse(octets[2], out long c) && long.TryParse(octets[3], out long d) { return (16777216L * a) + (65536L * b) + (256L * c) + d; } return null; } 

Or using a conditional operator (and using shift for simplicity):

 public static long? FromIpv4ToLong(this string ipAddress) { var octets = ipAddress.Split(IpSplitChar); return octets.Length == 4 && long.TryParse(octets[0], out long a) && long.TryParse(octets[1], out long b) && long.TryParse(octets[2], out long c) && long.TryParse(octets[3], out long d) ? (a << 24) | (b << 16) + (c << 8) | d : null; } 
+12
source

No, you cannot ignore this compile-time error or any compile-time error. You will need to make sure that the compiler is able to prove that no uninitialized local variable is ever read, you cannot just tell it to “trust”.

Fortunately, restructuring the code so that the compiler can prove that no uninitialized variable is ever read is not so difficult:

 public static long? FromIpv4ToLong(this string ipAddress) { var octets = ipAddress.Split(' '); if (octets.Length != 4) return null; if (long.TryParse(octets[0], out long a) && long.TryParse(octets[1], out long b) && long.TryParse(octets[2], out long c) && long.TryParse(octets[3], out long d)) { return ((16777216L * a) + (65536L * b) + (256L * c) + d); } return null; } 
+3
source

Try something like this:

 public static long? FromIpv4ToLong(this string ipAddress) { var octets = ipAddress.Split(IpSplitChar); if (octets.Length != 4) return null; if (long.TryParse(octets[0], out long a) && long.TryParse(octets[1], out long b) && long.TryParse(octets[2], out long c) && long.TryParse(octets[3], out long d)){ return ((16777216L * a) + (65536L * b) + (256L * c) + d); } return null; } 

The compiler is not smart enough to understand when success will be true.

+1
source

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


All Articles