Encoding type as a var
if (ra开发者_JAVA技巧dioButton1.Checked) {
var Enc = Encoding.Unicode;
}
var text = File.ReadAllText(filePath, (Enc);
It doesn't work, any way to make the encoding type a var so I can later p
The problem isn't using var
- it's that you've declared the variable inside a block, and then you're trying to use it outside the block.
Here's an alternative:
var encoding = Encoding.UTF8; // Default to UTF-8
if (useUtf16RadioButton.Checked)
{
encoding = Encoding.Unicode;
}
var text = File.ReadAllText(filePath, encoding);
The problem is that you must assign a value when you declare a variable with var so the type can be inferred (also you did specify Enc
only within the scope of the if condition so it couldn't be used afterwards):
var Enc = Encoding.UTF8; //default
if (radioButton1.Checked) {
Enc = Encoding.Unicode;
}
var text = File.ReadAllText(filePath, Enc);
精彩评论