跨越这行代码:
FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
Run Code Online (Sandbox Code Playgroud)
这两个问号意味着什么,是某种三元运算符?谷歌很难找到.
我很想撒谎并说英语是我的第二语言,但事实是我只是不知道'合并'是什么意思.我知道??C#中的"做什么",但这个名字对我来说没有意义.
我查了一下这个词,我理解它是'join'的同义词.'Null Join Operator'仍然没有意义.
有人可以开导我吗?
我想知道代码中的??标志C#.它是为了什么?我该如何使用它?
怎么样int??它是一个可以为空的int吗?
最近我有一个奇怪的错误,我在那里连接一个字符串int?然后添加另一个字符串.
我的代码基本上相当于:
int? x=10;
string s = "foo" + x ?? 0 + "bar";
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,这将运行和编译没有警告或不兼容的类型错误,这将是:
int? x=10;
string s = "foo" + x ?? "0" + "bar";
Run Code Online (Sandbox Code Playgroud)
然后这会导致意外的类型不兼容错误:
int? x=10;
string s = "foo" + x ?? 0 + 12;
Run Code Online (Sandbox Code Playgroud)
这个更简单的例子也是如此:
int? x=10;
string s = "foo" + x ?? 0;
Run Code Online (Sandbox Code Playgroud)
有人能解释一下这对我有用吗?
可能重复:
什么是"??"运算符?
我看到一行代码表明 -
return (str ?? string.Empty).Replace(txtFind.Text, txtReplace.Text);
Run Code Online (Sandbox Code Playgroud)
我想知道这条线的确切含义(即??部分)..
我正在看ASP.NET MVC 1.0生成的代码,并且想知道; 双重问号意味着什么?
// This constructor is not used by the MVC framework but is instead provided for ease
// of unit testing this type. See the comments at the end of this file for more
// information.
public AccountController(IFormsAuthentication formsAuth, IMembershipService service)
{
FormsAuth = formsAuth ?? new FormsAuthenticationService();
MembershipService = service ?? new AccountMembershipService();
}
Run Code Online (Sandbox Code Playgroud)
我正在努力学习单元测试.我正在尝试对我在asp.net mvc 1.0中制作的一些Memembership内容进行单元测试.我一直在关注一本关于MVC的书,我对一些希望有人可以为我清理的东西感到困惑.
我正在使用Nunit和Moq作为我的框架.
问题1:
public AuthenticationController(IFormsAuthentication formsAuth, MembershipProvider provider)
{
FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
Provider = provider ?? Membership.Provider;
}
Run Code Online (Sandbox Code Playgroud)
我有点困惑什么"??" 从来没有真正见过它.就像我甚至不知道这里发生了什么.就像他们通过界面然后"??" 标记发生并使一个新的FormsAuthenticationWraper成为?
问题2.
public AuthenticationController(): this(null, null)
{
}
Run Code Online (Sandbox Code Playgroud)
我知道这是默认的构造函数,但我不确定为什么":this(null,null)"正在做.
就像它实施的是什么?这也是什么意思.除此之外,为什么不能将其排除在外?并按原样坚持默认构造函数.
问题3.
在书中(很快就是asp.net mvc 1.0),它讨论了如何实现Memembership提供程序将会有很多工作要做.所以他们使用moq模型框架让生活更轻松.
现在我的问题是他们不使用"FormsAuthentication"上的moq.他们改为创建一个界面
public interface IFormsAuthentication
{
void SetAuthCookie(string userName, bool createPersistentCookie);
void SignOut();
}
Run Code Online (Sandbox Code Playgroud)
然后做一个包装
public class FormsAuthenticationWrapper:IFormsAuthentication {public void SetAuthCookie(string userName,bool createPersistentCookie){FormsAuthentication.SetAuthCookie(userName,createPersistentCookie); public void SignOut(){FormsAuthentication.SignOut(); }
}
Run Code Online (Sandbox Code Playgroud)
然后最后一个属性
public IFormsAuthentication FormsAuth
{
get;
private set;
}
Run Code Online (Sandbox Code Playgroud)
他们只有会员资格
public static MembershipProvider Provider …
c# ×6
coalesce ×2
.net ×1
asp.net-mvc ×1
moq ×1
nunit ×1
operators ×1
symbols ×1
terminology ×1
types ×1
unit-testing ×1