Ans: C# is a type-safe, managed and object oriented language, which is compiled by .Net framework for generating intermediate language (IL).
Ans: Below are the types of comments in C#:
Single Line Comment Eg : //
Multiline Comments Eg: /* */
XML Comments Eg : ///
Ans: In order to understand this, we must first analyze what benefits we can derive from OO. In order to do this, we must clearly understand its foundation. So given that C# is at its core object-oriented.
Ans: In OOPS we develop our code in modules. Each module has certain responsibilities. Cohesion shows how much a module responsibilities are strongly related.
Higher cohesion is always preferred. Higher cohesion benefits are:
Ans: Immutable means string values cannot be changed once they have been created. Any modification to a string value results in a completely new string instance, thus an inefficient use of memory and extraneous garbage collection. The mutable System.Text.StringBuilder class should be used when string values will change.
Ans: Array stores the values or elements of same data type but array list stores values of different data types.
Arrays will use the fixed length but array list does not uses fixed length like array.
Ans: As a developer, you might be tempted to answer that it comprises things like Encapsulation, Polymorphism, Abstraction, and Inheritance. Although this is true, it doesn’t really explain the fundamental core of what OO is and what its benefits are.
Principles are crucial but they are not the most important aspect of what OO actually is. What is really important is to understand in what grounds OO is built upon, or in other words, what are the foundations of OO programming.
The two most fundamental core concepts on which OO has been built upon in C# are this pointer and Dynamic Dispatch.
Obviously, there are principles like Encapsulation, Polymorphism, Abstraction, and Inheritance, but these are the consequence and not the generating force behind the OO paradigm in C#.
Ans: OOPS Modules are dependent on each other. Coupling refers to level of dependency between two software modules.
Two modules are highly dependent on each other if you have changed in one module and for supporting that change every time you have to change in dependent module.
Loose Coupling is always preferred.
Inversion of Control and dependency injections are some techniques for getting loose coupling in modules.
Ans: The Main method.
Ans: “Using” statement calls – “dispose” method internally, whenever any exception occurred in any method call and in “Using” statement objects are read only and cannot be reassignable or modifiable.
Ans: The this pointer is silently passed with a call to an instance-level function, which then operates on an object (instance of a class).
Basically, this core mechanism makes it possible to bring operations close to data. It also eliminates the need to have global functions and it gives data structures the intrinsic ability to perform operations on its data.
Ans: Abstraction is a technique of taking something specific and making it less specific.
In OOPS we achieve the abstraction by separating the implementation from interface. We take a implemented class and took only those method signatures and properties which are required by the class client. We put these method signatures and properties into interface or abstract class.
Ans: You put an @ sign in front of the double-quoted string.
String ex = @"This has a carriage return\r\n"
Ans: Namespaces are containers for the classes. We will use namespaces for grouping the related classes in C#. “Using” keyword can be used for using the namespace in other namespace.
Ans: What would your answer be? Pretty obvious. The humble this pointer.
Notice that despite this being a mind-bending idea, we can already start to appreciate the bigger picture for which C# was designed.
The this pointer is basically a way for a data structure (object) to be able to access methods that allow itself to perform operations on its own data. It is a way to manage state within a data structure.
Now let’s talk a bit about the other core concept that takes this to the next level.
Ans: In non object oriented languages, data and behaviors are not tied together. That means any function in the program can modify the data.
In Encapsulation, we bind the data and behaviors in one object. Only defined behaviors in a class can modify the data. We hide the state of an object by using properties and methods. Clients of the object only see these behaviors and by only these behaviors clients can modify the data.
We also protect the data by using access specifiers. We put the private / protected keywords before data to protect it from the outside world.
Ans: Structs cannot be inherited. Structs are passed by value and not by reference. Structs are stored on the stack not the heap. The result is better performance with Structs.
Ans: “Static” keyword can be used for declaring a static member. If the class is made static then all the members of the class are also made static. If the variable is made static then it will have a single instance and the value change is updated in this instance.
Ans: A singleton is a design pattern used when only one instance of an object is created and shared; that is, it only allows one instance of itself to be created. Any attempt to create another instance simply returns a reference to the first one. Singleton classes are created by defining all class constructors as private. In addition, a private static member is created as the same type of the class, along with a public static member that returns an instance of the class. Here is a basic example:
public class SingletonExample { private static SingletonExample _Instance; private SingletonExample () { } public static SingletonExample Get Instance() { if (_Instance == null) { _Instance = new SingletonExample (); } return _Instance; }}
Ans: “Finally” block will be executed irrespective of exception. So while executing the code in try block when exception is occurred, control is returned to catch block and at last “finally” block will be executed. So closing connection to database / releasing the file handlers can be kept in “finally” block.
Ans: Boxing is the process of explicitly converting a value type into a corresponding reference type. Basically, this involves creating a new object on the heap and placing the value there. Reversing the process is just as easy with unboxing, which converts the value in an object reference on the heap into a corresponding value type on the stack. The unboxing process begins by verifying that the recipient value type is equivalent to the boxed type. If the operation is permitted, the value is copied to the stack
Ans: Yes we can have only try block without catch block.
Ans: So now that we’ve explored these concepts, let’s move to a state-related code base. So you might be asking yourself at this moment.
Ans: “out” parameter can be passed to a method and it need not be initialized where as “ref” parameter has to be initialized before it is used.
Ans: Methods are overloaded via different signatures (number of parameters and types). Thus, you can overload a method by having different data types, different number of parameters, or a different order of parameters.
Ans: If the elements of an array is an array then it’s called as jagged array. The elements can be of different sizes and dimensions.
Ans: The sealed keyword prohibits a class from being inherited.
Ans: Inner exception is a property of exception class which will give you a brief insight of the exception i.e, parent exception and child exception details.
Ans: The GAC is the Global Assembly Cache. Shared assemblies reside in the GAC; this allows applications to share assemblies instead of having the assembly distributed with each application. Versioning allows multiple assembly versions to exist in the GAC—applications can specify version numbers in the config file. The gacutil command line tool is used to manage the GAC.
Ans: This is a situation where in, multiple resources are dependent on each other and this causes a lock condition and this makes the resource to be unused