简体   繁体   中英

Declaring var variables with Roslyn Sept 2012 CTP

There are other questions here which seem to address this question with older versions of the CTP, but they do not appear to work anymore with the latest release. I'm trying to figure out how to change the declaration from int to var. SyntaxKind.VarKeyword is no longer available to me.

Syntax.VariableDeclaration(
    Syntax.PredefinedType(Syntax.Token(SyntaxKind.IntKeyword)),
    Syntax.SeparatedList(Syntax.VariableDeclarator(Syntax.Identifier(name)))))

var is not actually a keyword in the language, so we removed it. You can simply create an identifier named "var" and that will work. Note however, that if there happened to be a type in scope named var, that would be used.

Something like:

Syntax.VariableDeclaration(
    Syntax.IdentifierName("var"),
    Syntax.SeparatedList(Syntax.VariableDeclarator(Syntax.Identifier(name))))

Though note that for type inference to work you will need to provide an EqualsValueClauseSyntax to you VariableDeclarator as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM