Exam Flashcards
What is Managed or Unmanaged Code in C#?
- Managed code is .NET code (VB.NET, C# etc.) that you write and compile to .NET CIL.
- Unmanaged code is code that is not under .NET that compiles to direct machine code.
What is Boxing and Unboxing in C#?
- Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap.
- Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit.
What is the difference between a struct and a class in C#?
- Structs are value types and classes are reference types.
- Structs share most of the same syntax as classes, but are more limited than classes.
- Unlike classes, structs can be instantiated without using a new operator.
- Structs do not support inheritance and cannot contain virtual methods.
What is the difference between “continue” and “break” statements in C#?
A break will exit the loop completely, continue will just skip the current iteration.
What is the difference between constant and read only in C#?
Constants:
can be assigned values only at the time of declaration
Constant variables must be accessed using “Classname.VariableName”
Constants are known at compile time
Read Only:
Read only variables can be assigned values either at runtime or at the time of instance initialization via constructor
Read only variables must be accessed using the “InstanceName.VariableName”
Read only variables are known at run time.
What is the difference between ref and out keywords in C#?
- ref tells the compiler that the object is initialized before entering the function
- out tells the compiler that the object will be initialized inside the function.
Define Property in C#.net?
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they actually include special methods called accessors. class Person { private string name; //field
public string Name //property { get { return name; } set { name = value; } } }
What is the difference between string and StringBuilder?
String:
String is immutable. Immutable means once we create string object we cannot modify. Any operation like insert, replace or append happened to change string simply it will discard the old value and it will create new instance in memory to hold the new value.
StringBuilder:
String builder is mutable it means once we create string builder object we can perform any operation like insert, replace or append without creating new instance for every time.
What is delegates in C#.
- A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.
- Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegateclass.
- List can contain 0, 1, or many method references
- Once a delegate is assigned a method, it behaves exactly like that method
That is, you can perform method calls, via the delegate
In the context of pub/sub systems, a delegate holds the list of subscribers
That is, the list of methods to call when an event occurs
Three pillars of Object-Oriented Programing (OOP) are Encapsulation, Inheritance, Polymorphism. Explain each of them and why they are important?
- Encapsulation refers to the hiding of data and implementation details.
- when encapsulating data and details into classes and objects programs become easier to manage and understand. (e.g methods encapsulated in classes)
- Encapsulation is implemented by using access modifiers. An access modifier defines the scope and visibility of a class member.
- Inheritance allows us to define a class based on another class. This makes creating and maintaining an application easy.
- The class whose properties are inherited by another class is called the Base class. The class which inherits the properties is called the Derived class
- polymorphism occurs when there is a hierarchy of classes and they are related through inheritance from a common base class.
- Polymorphism means that a call to a member method will cause a different implementation to be executed depending on the type of object that invokes the method.
- Simply, polymorphism means that a single method can have a number of different implementations.
Explain uses of delegates in C#?
Delegates are especially used for implementing events and the call-back methods
What is sealed class in c#?
The sealed keyword provides a level of protection to your class so that other classes cannot inherit from it.
Describe the differences between the public, private and protected access modifiers. Give an example of use for each type
The public access modifier makes the type or member accessible by any other code in the same assembly or any other assembly that references it. The private access modifier makes the type or member accessible only by code in the same class or struct. The protected access modifier is very similar to private with one difference; it can be accessed in the derived classes. So, a protected member is accessible only from derived classes.
What are partial classes?
- Partial class splits the definition of a class over two or more source files.
- You can create a class definition in multiple files, but it will be compiled as one class.
Explain how to create a read-only property for a class and provide example of use.
The readonly modifier prevents a member of a class from being modified after construction. It means that the field declared as readonly can be modified only when you declare it or from within a constructor. class Person { private readonly string name = "John"; public Person(string name) { this.name = name; } }
What is IEnumerable<> in c#?
It’s an interface implemented by Collection types in .NET that provide the Iterator pattern. There is also the generic version which is IEnumerable.
C# does not support multiple-inheritance? Explain why and what are the alternatives to multiple-inheritance?
NET and Java designers did not allow multiple inheritance because they reasoned that adding MI added too much complexity to the languages while providing too little benefit.
You can use interfaces instead.
What is the difference between checked and unchecked exceptions in c#
The checked keyword throws an overflowexception when a calculation causes an arithmatic overflow to occur
The unchecked keyword prevents the runtime from throwing an OverFlowException during execution and is the default.
What is the Constructor Chaining in C#?
Using a constructor to invoke another constructor, either within itself or in the base class
What’s the difference between the Array.CopyTo() and Array.Clone() in C#?
The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array
Can Multiple Catch Blocks execute in c#?
No, because only one of the exceptions will be thrown. Execution leaves the try block as soon as the exception is thrown, and assuming there’s a matching catch block, it continues there. It doesn’t go back into the try block, so you can’t end up with a second exception.
What is Singleton Design Patterns and How to implement in C#?
The singleton pattern is a software design pattern that is used to restrict instantiation of a class to one object. This is useful when we require exactly one object of a class to perform our operations. In this pattern we ensure that the class has only one instance and we provide a global point of access to this object. The normal implementation of singleton design pattern in C#like this. public class Singleton { private static Singleton _instance; private Singleton() { Console.WriteLine("Instance created"); }
public void Log(string message) { Console.WriteLine(message); }
public static Singleton Instance { get { if (null == _instance) { _instance = new Singleton(); }
return _instance; } } }
What is multicast delegate in c#?
Delegate objects can be composed using the “+” operator. A composed delegate calls the two delegates it was composed from. Only delegates of the same type can be composed. The “-“ operator can be used to remove a component delegate from a composed delegate.
Using this property of delegates you can create an invocation list of methods that will be called when a delegate is invoked. This is called multicasting of a delegate.
What is difference between Equality Operator (==) and Equals() Method in C#.
The == Operator compares the reference identity while the Equals() method compares only contents
How to use Nullable<> Types in .Net?
Nullable types are values which you can assign normal range of values as well as null values.
For example, you can store any value from -2,147,483,648 to 2,147,483,647 or null in a Nullable variable. Similarly, you can assign true, false, or null in a Nullable variable. Syntax for declaring a nullable type is as follows −
< data_type> ? = null;