开发者

Is this Legal Syntax for C# (ASP.NET)?

I know that in Windows Forms Desktop Applications, you can do this:

string name, favoriteColor, hairStyle;

name = "jason";
favoriteColor = "Dependand upon current mood.";
hairStyle = "Spikey";

But what about when using C# with ASP.NET websites?

You can do this:

var name = "jason";
var favoriteColor = "Dependand upon current mood.";
var hairStyle = "Spikey";

But is it also correct to do it like this?:

var name, favoriteColor, hairStyle;

name = "jason";
favoriteColor = "Dependand upon current mood.";
hairStyle = "Spikey";

The reason I am asking is because I cannot find anywhere in the docs where it says you should or should not do this. And I really dislike doing things like this:

var fname = "";
var lname = "";

@{
    fname = row.FirstName;
    lname = row.LastName;
}

I would much rather be doing thi开发者_开发技巧s:

var fname, lname;

@{
    fname = row.FirstName;
    lname = row.LastName;
}

Any help is appreciated.

Thank you


C# is C#, no matter where it's being run.

Anything you can do in desktop applications can also be done in ASP.Net, although it might not make any sense at runtime. (eg, don't show any forms)

However, the var keyword cannot be used to declare multiple variables on the same line, or to declare uninitialized variables; you need to explicitly declare the variable type.


You seem to be under the impression that var is related to ASP.Net or Razor.
It isn't.

The var keyword was introduced by C# 3 for anonymous types and LINQ and can be used anywhere (but not with multiple declarations)


It's not correct to do

var name, favoriteColor, hairStyle;

name = "jason";
favoriteColor = "Dependand upon current mood.";
hairStyle = "Spikey";

in C#, whatever environment you run in.

That's because implicitly-typed variables (i.e. where 'var' is used) must have their type available to the compiler at the point they're defined.


I think you cannot do:

var name, favoriteColor, hairStyle;

    name = "jason";
    favoriteColor = "Dependand upon current mood.";
    hairStyle = "Spikey";

because the type has to be know during assignment.


This is from MSDN documentation:

var can only be used when a local variable is declared and initialized in the same statement; the variable cannot be initialized to null, or to a method group or an anonymous function.

Reference:

Implicitly Typed Local Variables (C# Programming Guide)


Given this is ASP.NET, you can actually declare var as a type, and do both implementations (it's C# after all, doesn't matter how you paint it, it will try and compile) so both ways are correct. And no, doesn't matter how you write it as long as it is well documented and sticks to the code style you have been using.

But try not to overuse var, it is a lot better if you accustomed yourself to use a strong type all the time, instead of var, string (no caps, there is a subtle difference) or int, or whatever the app needs.

Also remember in web, things work slightly different. if you are assigning before the response, it should be fine, if it is an ajax response, do not forget to repaint the thing you want changed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜