相关疑难解决方法(0)

在C#中使用var关键字

在与同事讨论了在C#3中使用'var'关键字后,我想知道人们对通过var的类型推断的适当用途有什么看法?

例如,我在可疑情况下懒得使用var,例如: -

foreach(var item in someList) { // ... } // Type of 'item' not clear.
var something = someObject.SomeProperty; // Type of 'something' not clear.
var something = someMethod(); // Type of 'something' not clear.
Run Code Online (Sandbox Code Playgroud)

var的更合理用途如下: -

var l = new List<string>(); // Obvious what l will be.
var s = new SomeClass(); // Obvious what s will be.
Run Code Online (Sandbox Code Playgroud)

有趣的是LINQ似乎有点灰色,例如: -

var results = from r in dataContext.SomeTable
              select r; // Not *entirely clear* what results will be …
Run Code Online (Sandbox Code Playgroud)

c# var type-inference

406
推荐指数
32
解决办法
13万
查看次数

将JSON列表解析为C#中的int数组

我在将数字的JSON列表读取到ac#int []数组时遇到麻烦。

我尝试了SO的一些建议,但没有一个起作用。我将如何使用JSON.net进行此操作?

从JSON文件中提取:

    {
        "course": "Norsk",
        "grades": [6, 3, 5, 6, 2, 8]
    }
Run Code Online (Sandbox Code Playgroud)

我在c#中尝试过的内容:

// Reads the JSON file into a single string
string json = File.ReadAllText(jfile);
Console.WriteLine(json);

// Parsing the information to a format json.net can work with
JObject data = JObject.Parse(json);

JToken jToken = data.GetValue("grades");
jGrades = jToken.Values<int>().ToArray();
Run Code Online (Sandbox Code Playgroud)

和:

// Reads the JSON file into a single string
string json = File.ReadAllText(jfile);
Console.WriteLine(json);

// Parsing the information to a format json.net can work with
JObject data …
Run Code Online (Sandbox Code Playgroud)

c# json.net

0
推荐指数
1
解决办法
3543
查看次数

标签 统计

c# ×2

json.net ×1

type-inference ×1

var ×1