英文:
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<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);
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<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; // works: generic implementation assignable to raw interface
ilist = inums; // fails: error CS0266: Cannot implicitly convert type `System.Collections.Generic.IList<int>' to `System.Collections.IList'. 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<int>' to `System.Collections.ArrayList'
// List list = nums; // error CS0305: Using the generic type `System.Collections.Generic.List<T>' requires `1' type argument(s)
Console.WriteLine("list="+ alist);
ilist = istrs; // fails: error CS0266: Cannot implicitly convert type `System.Collections.Generic.IList<string>' to `System.Collections.IList'. An explicit conversion exists (are you missing a cast?)
alist = strs; // fails: error CS0029: Cannot implicitly convert type `System.Collections.Generic.List<string>' to `System.Collections.ArrayList'
Console.WriteLine("list="+ ilist);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论