Click here to Skip to main content
15,920,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

Is there an equivalent to the vbs switch "Option Explicit" in .NET (specifically C#)? I've been burned a couple times now by variables that I failed to explicitly initialize. I'd rather break than have code I think is working which produces bad output!!
Posted

There is no equivalent in C#.

In C, C++ and C# you must declare your variables before attempting to assign them.

You should however be aware of variable scope - Variable and Method Scope in Microsoft .NET[^] - there are no differences between scope in VB.NET and C# (AFAIK).

[EDIT] - Option Explicit in VB isn't what provides the default values - those are defined by the framework - Default Values Table (C# Reference)[^]

If you want to prevent default values being used then get into the habit of always explicitly assigning a value to the variable. There are tools available that will prompt you if you have not done this (I don't know of any free ones though)

[EDIT #2]
I downloaded an express version of Visual Studio so I could check this without the tools or settings I normally use...
Consider this code snippet ..
C#
int c;

private void button1_Click(object sender, EventArgs e)
{
    int a;
    int b = 10;

    Console.WriteLine((a + b).ToString());
    Console.WriteLine((c + b).ToString());

}

This gave me one error and one warning ...
Error Use of unassigned local variable 'a'
Warning 'WindowsFormsApplication1.Form1.c' is never assigned to, and will always have its default value 0

So for local variables you will be told off by VS for not initialising but for 'global' or class level variables you will only be warned by default.

You can force VS to convert those warnings to errors by going into YourProject, Properties, Build
 
Share this answer
 
v3
Comments
CertNerd 4-Feb-16 13:16pm    
Thanks for the input!

In my case I don't receive any warnings probably because I did assign a value to each variable somewhere in the code, but not in each and every possible logic path.

For instance:

bool keepThisRecord;

if (!validRecord())
keepThisRecord = false;

Obviously, in this case, 'keep' will *always* be false. But if you're careless like me, you don't know that until after you get bad output :)
CHill60 5-Feb-16 7:51am    
I make it a habit of assigning a value at the point of declaration e.g. bool keepThisRecord = false; - it can be a bit of overkill sometimes but I can always go back later and tidy up than miss something.
Visual Studio will also warn you if you haven't assigned a local variable with a "Use of unassigned local variable" error. It doesn't do that with class level/global variables though - although good practice is to avoid their use where possible.
In C#: in the Project Properties facility, in the 'Build options panel you can set the "warning level" that will govern what types of error events (messages) will be raised: [^]:

Warning level Meaning
0 Turns off emission of all warning messages.
1 Displays severe warning messages.
2 Displays level 1 warnings plus certain, less-severe warnings, such as warnings about hiding class members.
3 Displays level 2 warnings plus certain, less-severe warnings, such as warnings about expressions that always evaluate to true or false.
4 Displays all level 3 warnings plus informational warnings. This is the default warning level at the command line.


Work in Debug mode.
 
Share this answer
 
Option Explicit in VB.NET only forces you to declare variables before you try to use them. That's it. Nothing else. It has nothing to do with initializing variables. All variables in .NET are initialized to an appropriate value by default.
Public Function TestSub(x As Integer, y As Integer)
    ' The following line will cause a compiler error if Option Explicit is turned on.
    ' i was never defined before being used.
    i = x + y
    return i
End Function


C# forces this by default. There is no turning it off.
public int TestSub(int x, int y)
{
    i = x + y;
    return i;
}

will throw an error because i wasn't declared at all.

Option Explicit does NOT force you to initialize a variable. There is no option in VB.NET or C# that does this.

If the default value of a variable causes your code to give unexpected results, the problem is with you, not some option. You didn't take the steps required to validate your input before you tried to use those values. Input, in this context, is any values being used by an algorithm, including declared variables, not just the stuff a user types in.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900