1.Explain how bytecode (either CIL or Java bytecode) differs from machine code with examples of instructions. 2.Why autoboxing and autounboxing is not needed in C#? 3.How to decide when and when not to use auto-implemented properties in C#? 4.What is the difference between structs and class in C#? 5.How to decide between using a regular parameter declaration and a ref parameter declaration in a C# method? 6.What is the difference between modifiers new and override in C#? 7.How to decide between using a two-dimensional array and an array of arrays in a programming situation in C#? 8.Discuss the common errors that can arise with switch statements in Java and how C# eliminates those errors automatically. 9.How to use foreach loop in C#? Give one example. 10.What does this program print on the screen? Explain your answer by describing what a delegate is, including the meaning of +=. class C { delegate bool Check (int number); static bool IsEven (int number) { return number % 2 == 0;} static bool IsSmallerThan3 (int number) { return number < 3;} static void Main () { Check f = IsEven; f += IsSmallerThan3; Console.WriteLine ( f(3)); Console.WriteLine ( f(2)); } }
时间: 2024-02-14 09:07:34 浏览: 92
1. Bytecode is an intermediate code that is generated by a compiler to be executed by a virtual machine. It is platform-independent and can be executed on any platform that has a virtual machine installed. Machine code, on the other hand, is the binary code that is directly executed by a computer's CPU. Here are some examples of instructions in bytecode and machine code:
- Bytecode (CIL): ldloc.0 (load local variable 0), ldc.i4.5 (load constant value 5), add (addition)
- Machine code: mov eax, [ebp-4] (move value from memory to register), mov ebx, 5 (move constant value to register), add eax, ebx (addition)
2. Autoboxing and autounboxing is not needed in C# because C# has a feature called "value types" that allows the value types to be treated like objects. This means that value types can be used in collections and passed as parameters to methods without the need for autoboxing and autounboxing.
3. Auto-implemented properties should be used when there is no additional logic required for the getter or setter methods. If additional logic is required, then a regular property with custom getter or setter methods should be used.
4. Structs and classes are similar in that they both can contain fields, methods, and properties. The main difference is that structs are value types, while classes are reference types. This means that structs are passed by value, while classes are passed by reference.
5. A regular parameter declaration should be used when the method only needs to read the value of the parameter. A ref parameter declaration should be used when the method needs to modify the value of the parameter.
6. The "new" modifier is used to hide a method or property in a derived class, while the "override" modifier is used to replace a method or property in a base class with a new implementation in a derived class.
7. A two-dimensional array should be used when the data is logically a matrix or a table with rows and columns. An array of arrays should be used when the data is logically a collection of arrays.
8. In Java, the switch statement can cause errors if there are duplicate case values or if the break statement is not used after each case. In C#, the switch statement automatically eliminates these errors by requiring a default case and not allowing fall-through cases.
9. The foreach loop is used to iterate over a collection of items, such as an array or a list. Here is an example:
```
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}
```
10. The program will print "True" and "False" because the delegate "f" contains two methods, "IsEven" and "IsSmallerThan3", which are both called when "f" is invoked with a parameter. The "+=" operator is used to add the "IsSmallerThan3" method to the delegate "f". A delegate is a type that encapsulates a method or a group of methods, and the "+=" operator is used to add a method to a delegate's invocation list.
阅读全文