什么类型的 A 和 B,在 Java 中是合法的简单赋值语句,但在 C# 中却不合法?

huangapple go评论80阅读模式
英文:

For what types of A and B is the simple assignment statement A = B legal in Java but not C#?

问题

以下是您需要的翻译内容:

在我的作业中遇到了这个问题,尝试过在谷歌上搜索,但没有任何结果。

例如,在C++中允许的简单赋值语句 A = B,在Java中却不允许:

bool a = 1; // 在C++中合法

但是

boolean a = 1; // 在Java中不合法

我想知道:
对于在Java中合法但在C#中不合法的哪种 A 和 B 的类型,简单的赋值语句 A = B 是合法的。

英文:

Got this question in my homework, tried googling but no luck there either.

For example, simple assignment statement A = B legal in C++ but not Java:

bool a = 1; is legal C++.

But

boolean a = 1; is not legal Java.

I want to know
For what types of A and B is the simple assignment statement A = B legal in Java but not C#?

答案1

得分: 0

在Java和C#中,实现通用类的方式不同,因此在Java中,可以将带类型的集合分配给原始集合(尽管会得到警告),或者分配给带通配符的集合(感谢Johannes Kuhn):

ArrayList<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
List<Integer> inums = nums;
      
ArrayList<String> strs = new ArrayList<>(Arrays.asList("a", "bb", "ccc"));
List<String> istrs = strs;
      
ArrayList<?> list = nums;
List ilist = inums;
      
System.out.println("implementation: " + list);
System.out.println("interface: " + ilist);
      
list = strs;
ilist = istrs;

System.out.println("implementation: " + list);
System.out.println("interface: " + ilist);

这段代码将正常工作并打印列表。

在C#中,可以将通用实现分配给原始接口引用。

然而,不能将通用接口分配给原始接口,也不能将通用实现分配给原始实现,如下所示:

static void Main() {
    List<int> nums = new List<int>();
    nums.Add(1);
    nums.Add(2);
    nums.Add(3);
    nums.Add(4);
    IList<int> inums = nums;
    
    List<string> strs = new List<string>();
    strs.Add("aa");
    strs.Add("bbb");
    IList<string> istrs = strs;
    
    System.Collections.IList ilist = nums; // 成功:通用实现可分配给原始接口
    ilist = inums; // 失败:错误 CS0266:无法隐式地将类型 `System.Collections.Generic.IList<int>' 转换为 `System.Collections.IList'。存在显式转换(是否缺少转换?)
 
    // 将通用实现分配给原始实现
    System.Collections.ArrayList alist = nums; // 失败:错误 CS0029:无法隐式地将类型 `System.Collections.Generic.List<int>' 转换为 `System.Collections.ArrayList'
    // List list = nums; // 错误 CS0305:使用通用类型 `System.Collections.Generic.List<T>' 需要 `1' 个类型参数

    Console.WriteLine("list=" + alist);
        
    ilist = istrs; // 失败:错误 CS0266:无法隐式地将类型 `System.Collections.Generic.IList<string>' 转换为 `System.Collections.IList'。存在显式转换(是否缺少转换?)
    alist = strs;  // 失败:错误 CS0029:无法隐式地将类型 `System.Collections.Generic.List<string>' 转换为 `System.Collections.ArrayList'
    Console.WriteLine("list=" + ilist);
}
英文:

Implementation of generic classes is different in Java and C#, so in Java it is possible to assign a typed collection to a raw collection (though getting a warning) or to a collection with a wildcard (thanks Johannes Kuhn):

ArrayList&lt;Integer&gt; nums = new ArrayList&lt;&gt;(Arrays.asList(1, 2, 3, 4, 5));
List&lt;Integer&gt; inums = nums;
      
ArrayList&lt;String&gt;  strs = new ArrayList&lt;&gt;(Arrays.asList(&quot;a&quot;, &quot;bb&quot;, &quot;ccc&quot;));
List&lt;String&gt; istrs = strs;
      
ArrayList&lt;?&gt; list = nums;
List ilist = inums;
      
System.out.println(&quot;implementation: &quot; + list);
System.out.println(&quot;interface: &quot; + ilist);
      
list = strs;
ilist = istrs;

System.out.println(&quot;implementation: &quot; + list);
System.out.println(&quot;interface: &quot; + ilist);

This code will work and print lists.

In C# it's possible to assign generic implementation to raw interface reference.

However, generic interface cannot be assigned to raw interface and generic implementation cannot be assigned to raw implementation as shown below:

static void Main() {
    List&lt;int&gt; nums = new List&lt;int&gt;();
    nums.Add(1);
    nums.Add(2);
    nums.Add(3);
    nums.Add(4);
    IList&lt;int&gt; inums = nums;
    
    List&lt;string&gt; strs = new List&lt;string&gt;();
    strs.Add(&quot;aa&quot;);
    strs.Add(&quot;bbb&quot;);
    IList&lt;string&gt; istrs = strs;
    
    System.Collections.IList ilist = nums; // works: generic implementation assignable to raw interface
    ilist = inums; // fails:  error CS0266: Cannot implicitly convert type `System.Collections.Generic.IList&lt;int&gt;&#39; to `System.Collections.IList&#39;. An explicit conversion exists (are you missing a cast?)
 
    // assigning generic implementation to raw implementation
    System.Collections.ArrayList alist = nums; // fails: error CS0029: Cannot implicitly convert type `System.Collections.Generic.List&lt;int&gt;&#39; to `System.Collections.ArrayList&#39;
    // List list = nums; // error CS0305: Using the generic type `System.Collections.Generic.List&lt;T&gt;&#39; requires `1&#39; type argument(s)

    Console.WriteLine(&quot;list=&quot;+ alist);
        
    ilist = istrs; // fails: error CS0266: Cannot implicitly convert type `System.Collections.Generic.IList&lt;string&gt;&#39; to `System.Collections.IList&#39;. An explicit conversion exists (are you missing a cast?)
    alist = strs;  // fails: error CS0029: Cannot implicitly convert type `System.Collections.Generic.List&lt;string&gt;&#39; to `System.Collections.ArrayList&#39;
    Console.WriteLine(&quot;list=&quot;+ ilist);
}

huangapple
  • 本文由 发表于 2020年9月21日 06:18:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63984171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定