Sunday, November 30, 2008

CSharp Question

1. What is the name of the entry point function for all C# programs?

The main method is the name of the entry point function for all C# programs.

2. The C# data type int is a synonym for what CLR data type?

Data type int is a synonym for Int32 data type in the C#. (System.Int32)

3. In C# all primitive data types and user defined data types inherit from what super object?

They inherit from object.

4. How is the C# string class use of == different from all other classes?

All other classes will compare their identity comparison which will only return true if both references point to the same object. However, in string class, == is an overloading operator that compares the characters in the string. (not the reference values of them). Thus, if str1 = “abc” and str2=”abc”, str1 == str2 will return true even though str1 points to different object than str2.

5. What encoding does C# use for characters?

It uses 16-bit encoding. (Unicode)

6. What is the difference between the C# "ref" and "out" keywords when applied to method parameters?

The difference between the C# ref and out keywords when applied to method parameters:

- ref keyword is used to pass variable that have been initialized (have a value to it) by reference to a method, and the method will be able to modify the original variable in the caller.

- Out keyword is used to pass variable that have not been initialized ( does not have any value) by reference to a method, and the method will assign value to it.

In code:

Int a = 5;

Int b;

// If we want to pass a and b by reference

Method_Ref( ref a); // must use ref keyword because a have value

Method_Out( out b); // must use out keyword because b does not have value

7. What are the C# "checked" and "unchecked" keywords used for?

The checked keyword is used to control if the overflow-checking throws an exception. And, the unchecked keyword is used to control if the overflow-checking throws truncates bits.

8. What is the difference between a C# "using" directive and a C/C++ "#include" directive?

“using” directive helps the compiler locate a class that is used in this application. This class is predefined class that you can reuse. using directive identifies predefined classes or namespace that a C# application should be able to use. On the other hand, “#include” directive tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears. Thus, using directive only helps compiler to locate a predefined classes or namespace, while #include directive is like adding code to the program.

9. What is the difference between a C# struct and a C# class in terms of reference types and value types?

A struct in C# represents value type object. And, a class in C# is a reference type object.

10. Besides "public" and "private", what other two access modifiers can a C# class take?

Two other access modifiers are: “protected” and “internal”.

11. Why does C# use class destructors far less often that C++?

They have garbage collector (GC) mechanism.

12. In C#, are static methods accessed through a class name, an object name, or both?

It can only be accessed through class name only.

13. How do you make a C# class abstract (when you want to inherit from it but never implement it directly)?

Use the abstract modifier keyword in base class.

For example: public abstract void Draw(); // this method will not provide implementation.

14. How do you prevent a C# class from being used as a base class (inherited from)?

We need to use sealed modifier keyword.

15. C# does not support multiple inheritance. What C# mechanism allows you to have a semblance of multiple inheritance functionality?

C# mechanism that allows us to have a semblance of multiple inheritance is interfaces mechanism.

16. What are the two kinds of C# properties?

They are get and set.

17. Syntactically, what is the difference between calling a method and a property?

The difference is:

- calling a method: we need to use parentheses after method name.

ie: Console.WriteLine();

- calling a property: we don’t need to use parentheses after property variable.

ie: myClass.Name;

18. What is the approximate C# equivalent to a C++ function pointer?

The approximate c# equivalent to a C++ function pointer is delegate.

19. What C# keyword do you use to implement a variable length argument list?

C# keyword to implement a variable length argument list is params

20. In C# if you must use pointers, how do you do it?

We need to use unsafe keyword

No comments: