I've written a small code that uses an OCR. I've come across a confusing situation where my variables of datatype var get initialized before even the code reaches to the point where they're being initialized. Please have a look at this screenshot
the line of code RecAPIPlus.RecInitPlus(null,null);
is supposed to return status of API initialization. This line hasn't even executed but my status variable seems to have a default value ie REC_OK that the above line of code was supposed to return when it executes.
Can anyone explain the reason of this? Here's the complete code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nuance.OmniPage.CSDK;
namespace OmniPage
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine(Nuance.OmniPage.CSDK.ZONETYPE.WT_AUTO);
var status=RecAPIPlus.RecInitPlus(null,null);
IntPtr a;
var output = RecAPI.kRecLoadImgF(0, "out.txt", out a, 1);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e);
}
}
}
}
I guess the data type of the status
field is an enum
. The first value of the enum is the default value (if the enum is unnumbered, else the 0
value is).
var
just means "use the data type returned from that method (or assignment in general). So var
here means just "the enum type returned".
That means that if REC_OK
is the first value in the enum, it is the default value of that field. Enum's can't have a null value so it shows the default value.
I see the break point didn't come there yet. It still shows the current state of those uninitialed variables in the scope. That is just a Visual Studio thing.
status
variable is Enum
and enum
is value type, not reference type. Value types always have default values set even though your code has not reached the point were the variable is set.
Moreover, var
is not type, it is just syntax sugar and you variable has real enum
type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.