StringBuilder initializer works in VS2015, but different in VS2013

I have the following line of code with works in VS 2015 and .Net 4.0, but I get an error message in VS 2013.

StringBuilder s = new StringBuilder("test") {[0] = 'T'};

Why does it work differently?

+4
source share
2 answers

Basically, object initializer expressions do not support indexing in C # 5 (which supports VS2013). This is a new feature in C # 6.

In C # 6, your code is equivalent:

StringBuilder tmp = new StringBuilder(); // Compiler-generated 
tmp[0] = 'T';
StringBuilder s = tmp;

There is no equivalent to a single expression in C # 5, although of course you can just use new StringBuilder("T")to achieve the same result ...

Dictionary<,> - , " ", Add:

var dict = new Dictionary<string, int>
{
    { "key1", 10 },
    { "key2", 20 }
};

, :

var dict = new Dictionary<string, int>
{
    ["key1"] = 10,
    ["key2"] = 20
};

... , , , Dictionary<,>... Add . , , Add , .

, , , () Add. , , .

# 6 . Roslyn Codeplex.

+8

# 6, , Visual Studio 2013, .

, # 6:

var cppHelloWorldProgram = new Dictionary<int, string>
{
  [10] = "main() {",
  [20] = "    printf(\"hello, world\")",
  [30] = "}"
};

, 10, 20 30.

+1

Source: https://habr.com/ru/post/1568176/


All Articles