简体   繁体   English

这是C#(ASP.NET)的法律语法吗?

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

I know that in Windows Forms Desktop Applications, you can do this: 我知道在Windows Forms桌面应用程序中,您可以执行以下操作:

string name, favoriteColor, hairStyle;

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

But what about when using C# with ASP.NET websites? 但是,如何将C#与ASP.NET网站一起使用呢?

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 this: 我宁愿这样做:

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. C#是C#,无论它在哪里运行。

Anything you can do in desktop applications can also be done in ASP.Net, although it might not make any sense at runtime. 在桌面应用程序中可以执行的任何操作也可以在ASP.Net中完成,尽管在运行时可能没有任何意义。 (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; 但是, var关键字 不能用于在同一行上声明多个变量 ,也不能声明未初始化的变量。 you need to explicitly declare the variable type. 您需要显式声明变量类型。


You seem to be under the impression that var is related to ASP.Net or Razor. 您似乎对 var与ASP.Net或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) var关键字是C#3针对匿名类型和LINQ引入的,可以在任何地方使用(但不能与多个声明一起使用)

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. 在C#中,无论您在什么环境中运行。

That's because implicitly-typed variables (ie where 'var' is used) must have their type available to the compiler at the point they're defined. 这是因为隐式类型的变量(即在其中使用“ var”的位置)必须在定义点对编译器可用。

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: 这来自MSDN文档:

var can only be used when a local variable is declared and initialized in the same statement; var只能在同一语句中声明和初始化局部变量时使用 the variable cannot be initialized to null, or to a method group or an anonymous function. 该变量不能初始化为null,方法组或匿名函数。

Reference: 参考:

Implicitly Typed Local Variables (C# Programming Guide) 隐式类型局部变量(C#编程指南)

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. 鉴于这是ASP.NET,您实际上可以将var声明为一种类型,并同时执行两种实现(毕竟是C#,与绘制方式无关,它将尝试编译),因此两种方法都是正确的。 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. 但是请尽量不要过度使用var,如果您一直习惯使用强类型,而不是var,string(无大写字母,有细微的差别)或int或应用程序所需的任何类型,那会好得多。

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. 如果您要在响应之前进行分配,那应该没问题,如果是ajax响应,请不要忘记重新绘制要更改的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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