如何在C#中将空会话值默认为空字符串

时间:2022-06-26 11:48:32

I'm used to using VB.net for web programming.

我习惯使用VB.net进行网页编程。

Often, I have something like:

通常,我有类似的东西:

Dim s as string = Session("s")

I get a string value for s from the web session. If there is no value in the web session, I get a blank string.

我从Web会话中获取s的字符串值。如果网络会话中没有值,我会得到一个空白字符串。

However, AFAIK, in C#, I have to have something like the code below to do the same thing.

但是,AFAIK,在C#中,我必须有类似下面的代码才能做同样的事情。

string s;
try { s = Session["s"].ToString(); }
catch { s = ""; }

Is there an easier way to do this?

有更简单的方法吗?

3 个解决方案

#1


15  

This is a quick way of doing this:

这是一种快速的方法:

s = (string)Session["s"] ?? "";

This will cast Session["s"] to a string, and if it is not null, return the value. If it is null, it will return an empty string. The "a ?? b" expression is essentially the same as "a != null ? a:b" (?? is compiled more efficiently though)

这会将Session [“s”]强制转换为字符串,如果不为null,则返回该值。如果为null,则返回空字符串。 “a ?? b”表达式与“a!= null?a:b”基本相同(尽管可以更有效地编译??)

Something else to keep in mind: you should never use exceptions for normal application logic.

要记住的其他事项:您不应该对正常的应用程序逻辑使用异常。

#2


0  

Because string is reference type then is nullable, so you can check for empty or null by means of string.IsNullOrEmpty(s):

因为string是引用类型,所以可以为空,所以你可以通过string.IsNullOrEmpty(s)来检查空或null:

string s = string.IsNullOrEmpty((string)strObject) ? string.Empty : strObject.ToString();

Otherwise (as Philippe Leybaert says) you can use ?? operator.

否则(正如Philippe Leybaert所说)你可以使用??运营商。

#3


-3  

I almost agree to Philippe, but it only works if "s" is present in the session, otherwise there will be a KeyNotFoundException. This code checks it, but does not solve the NULL issue of Philippe.

我几乎同意Philippe,但它只有在会话中出现“s”时才有效,否则会出现KeyNotFoundException。此代码检查它,但不解决Philippe的NULL问题。

s= Session.ContainsKey("s")?Session["s"]:"";

So to cover both possibilities, it becomes mre complex:

因此,为了涵盖这两种可能性,它变得复杂:

s = Session.ContainsKey("s")?(Session["s"]??""):"";

It does not really make it easier, but the performance should be better than catching an exception.

它并没有真正使它更容易,但性能应该比捕获异常更好。

#1


15  

This is a quick way of doing this:

这是一种快速的方法:

s = (string)Session["s"] ?? "";

This will cast Session["s"] to a string, and if it is not null, return the value. If it is null, it will return an empty string. The "a ?? b" expression is essentially the same as "a != null ? a:b" (?? is compiled more efficiently though)

这会将Session [“s”]强制转换为字符串,如果不为null,则返回该值。如果为null,则返回空字符串。 “a ?? b”表达式与“a!= null?a:b”基本相同(尽管可以更有效地编译??)

Something else to keep in mind: you should never use exceptions for normal application logic.

要记住的其他事项:您不应该对正常的应用程序逻辑使用异常。

#2


0  

Because string is reference type then is nullable, so you can check for empty or null by means of string.IsNullOrEmpty(s):

因为string是引用类型,所以可以为空,所以你可以通过string.IsNullOrEmpty(s)来检查空或null:

string s = string.IsNullOrEmpty((string)strObject) ? string.Empty : strObject.ToString();

Otherwise (as Philippe Leybaert says) you can use ?? operator.

否则(正如Philippe Leybaert所说)你可以使用??运营商。

#3


-3  

I almost agree to Philippe, but it only works if "s" is present in the session, otherwise there will be a KeyNotFoundException. This code checks it, but does not solve the NULL issue of Philippe.

我几乎同意Philippe,但它只有在会话中出现“s”时才有效,否则会出现KeyNotFoundException。此代码检查它,但不解决Philippe的NULL问题。

s= Session.ContainsKey("s")?Session["s"]:"";

So to cover both possibilities, it becomes mre complex:

因此,为了涵盖这两种可能性,它变得复杂:

s = Session.ContainsKey("s")?(Session["s"]??""):"";

It does not really make it easier, but the performance should be better than catching an exception.

它并没有真正使它更容易,但性能应该比捕获异常更好。