开发者

Encoding type as a var

开发者 https://www.devze.com 2023-02-20 03:53 出处:网络
if (ra开发者_JAVA技巧dioButton1.Checked) { var Enc = Encoding.Unicode; } var text = File.ReadAllText(filePath, (Enc);
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);
0

精彩评论

暂无评论...
验证码 换一张
取 消