C# programming language


C# programming language 
 The C# programming language is based on Microsoft .NET, the popular platform in the Windows ecosystem. Many companies focusing on Windows development include requirements on C# skills in their job descriptions.
C# can be viewed as a managed programming language based on C++, so there are many similarities between them. It does not take much time for a C++ programmer to learn C#. It is easy to learn the similarities, but it is difficult to distinguish differences. Many interviewers like to ask questions about the confusing differences. For example, the following is a piece of dialog from an interview: 

Interviewer:    Types in C++ can be defined as struct and class. What are the differences between these two types?
Candidate:      The default access level in a struct is public, while in a class is private.
Interviewer:    How about in C#?
Candidate:     It is different in C#. The default access level for both struct and class is private in C#. However, their memory allocation models are different. A struct is a value type, and its instances are created on the stack. A type declared as a class is a reference type, and its instances are created on the heap.

Similar to C++, every type in C# has a constructor at least. Additionally, there might be two different methods (finalizer and Dispose) used to release resources in C#. The finalizer method looks similar to the destructor in C++, which is the type name followed by a complement symbol (~). However, the time to invoke them is different. The time to invoke a destructor in C++ is fixed, but the time to invoke the finalizer in C# is determined by the runtime. The finalizer is invoked by the garbage collector, and the time is unknown to programmers.
Additionally, there is a special constructor in C#, named static constructor. It is invoked automatically when its type is executed by the runtime at the first time, and it is invoked only once. There are many interesting interview questions about the static constructor. For example: What is the output of the C# code in Listing 2-12?
Listing 2-12. C# Code about Static Constructors
class A {
    public A(string text) {
        Console.WriteLine(text);
    }
}
class B{
    static A a1 = new A("a1");
    A a2 = new A("a2");
  static B() {
        a1 = new A("a3");
}
    public B() {
        a2 = new A("a4");
} }
class Program {
    static void Main(string[] args) {
B b = new B(); }
}
Before any code of the type B gets executed, its static constructor will be invoked first. It initializes static data fields and then executes statements inside the static constructor. Therefore, it prints a line of “a1”, and then another line of “a3”.
When the execution flow reaches the statement B b = new B(), the ordinary constructor is invoked. It initializes its data fields first and then the statements in its constructor method, so it prints a line of “a2”, and then another line of “a4”.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community




No comments:

 
Top Blogs