正确选择过载

时间:2018-07-11 07:52:26

标签: c#

我有验证码

signinUser(email: string, password: string) {
    return firebase.auth().signInWithEmailAndPassword(email, password)
        .then((response) => {
            this.router.navigate(['/']);
            return firebase.auth().currentUser.getIdToken()
                .then((token: string) => {
                    this.token = token
                    return token;
                })
        })
        .catch(
            error => console.log(error)
        );
}

为什么第一个运行并且第二个抛出异常? 为什么选择

//this runs string[] s_items = {"0","0"}; string s_output = string.Format("{0}{1}",s_items); //this one throws a exception int[] i_items = {0,0}; string i_output = string.Format("{0}{1}",i_items); Format(String, Object)超载

int[] Format(String, Object[])超载

2 个答案:

答案 0 :(得分:7)

string[]可以转换为object[],因为它们都是引用类型的数组。并且所有引用都是“相等的”。这是从第一天开始就内置到C#语言中的讨厌的(数组)转换之一,它不应该存在,但是从第一天开始我们就没有泛型和适当的协/逆规则。

int[]不能转换为object[],因为实际上第一个数组中包含的int不是引用。

答案 1 :(得分:3)

从msdn文档中,

  

这是编译器重载解析的问题。由于编译器无法将整数数组转换为对象数组,因此它将整数数组视为单个参数,因此将调用Format(String,Object)方法。

查看更多here

相关问题