You are on page 1of 25

Technical Interview Preparation Material

For .Net Technical Interviews

C# and OO Concepts
Object Oriented Programming
It is a problem solving technique to develop software systems. Its a technique to think real world in terms of objects. Object maps the software model to real world concept. These objects have responsibilities and provide services to application or other objects.

C# and OO Concepts
Class : A class describes all the attributes of objects , as well as the methods that implement the behavior of member objects. Its a comprehensive data type which represent a blue print of objects. Its a template of object. A Class is a user-defined data type that contains a collection of objects of similar types. Once a class is defined, any number of objects can be created, which belongs to that class. A class definition contains member variables and methods, which can be accessed in the Main() method by using the class object. The Syntax to define a class is as follows: [access-modifier] class <class-name> { Variables-declaration; Method-declaration; }

C# and OO Concepts
Object: Its a basic unit of a system. An object is an entity that has attributes, behavior, and identity. Objects are members of a class. Attributes and behavior of an object are defined by the class definition. A class encapsulates the variables and the methods, which can be accessed by using an object. In simple words, you must create an object of a class to access its member variables and methods. All objects of the class have their own copies of the variables and the methods defined in the class. For example, if you create two objects, named C1 and C2 of class Count, both objects can access the variables and the methods of this class separately by using their own copy of variables and methods. The Syntax to create an object in C# is as follows: <class-name> <object-name> = new <class-name> <object-name>

1.C# and OO Concepts


What is the relation between Classes and Objects?
They look very much same but are not same. Class is a definition, while object is a instance of the class created. Class is a blue print while objects are actual objects existing in real world.

1.C# and OO Concepts


What are different properties provided by Object-oriented systems ? Following are characteristics of Object Oriented Systems : Abstraction It allows complex real world to be represented in simplified manner. Example color is abstracted to RGB.By just making the combination of these three colors we can achieve any color in world.Its a model of real world or concept. Encapsulation The process of hiding all the internal details of an object from the outside world. Polymorphism When inheritance is used to extend a generalized class to a more specialized class,it includes behavior of the top clas(Generalized class).The inheriting class often implement a behavior that can be somewhat different than the generalized class, but the name of the behavior can be same.It is important that a given instance of an object use the correct behavior, and the property of polymorphism allows this to happen automatically.

1.C# and OO Concepts


Inheritance Inheritance is a principal of OOP in which a class inherits the features from some another class. A class inherits the features, such as member variables and methods. Hierarchy is used to define more specialized classes based on a preexisting generalized class.Example we have VEHICLE class and we can inherit this class make more specialized class like CAR, which will add new attributes and use some existing qualities of the parent class. Its kind of parent child relationship.

1.C# and OO Concepts


Abstract Classes
We can not create a designed to act as a base class (to be inherited by other classes). Abstract class is design concept in program development and provides a base upon which other classes are built. Abstract classes are similar to interface. After declaring an abstract class, it cannot be instantiated on its own, it must be inherited. In VB.NET, abstract class are created using MustInherit keyword. In C# we have Abstract keyword. Following are features of a abstract class : You can not create a object of abstract class Abstract class is designed to act as a base class (to be inherited by other classes). Abstract class is a design concept in program development and provides a base upon which other classes are built. Abstract classes are similar to interfaces. After declaring an abstract class, it cannot be instantiated on it's own, it must be inherited. In C# .NET abstract classes are created using Abstract keyword. Abstract classes can have implementation or pure abstract methods which should be implemented in the child class.

1.C# and OO Concepts


Interface
Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world , that it provides specific behavior . Example if a class is implementing Idisposable interface that means it has a functionality to release unmanaged resources . Now external objects using this class knows that it has contract by which it can dispose unused unmanaged objects. An interface can contain one or more methods, properties, indexers and events but none of them are implemented in the interface itself. Member declarations will contain only a list of members without implementation code. Single Class can implement multiple interfaces. If a class implements a interface then it has to provide implementation to all its methods..

What are the difference between Interface & abstract classes? Abstract classes can have concrete methods while interfaces have no methods implemented. Interfaces do not come in inheriting chain , while abstract classes come in inheritance Refer next slide for all the differences

Can we define variables in interfaces? No Can we define static members in interfaces? No Can we apply access modifiers in the methods of interface? No Can we implement more than one interface in a class? Yes

1.C# and OO Concepts


Feature Interface A class may implement several interfaces. An interface cannot provide any code at all, much less default code Abstract class A class may extend only one abstract class. An abstract class can provide complete code, default code, and/or just stubs that have to be overridden. Both instance and static constants are possible. Both static and instance intialiser code are also possible to compute the constants.

Multiple Inheritance Default implementation Constants

Static final constants only, can use them without qualification in classes that implement the interface. On the other paw, these unqualified names pollute the namespace. You can use them and it is not obvious where they are coming from since the qualification is optional.

Third party convenience


is-a vs -able or can-do

An interface implementation may be added to any existing third party class.


Interfaces are often used to describe the peripheral abilities of a class, not its central identity, e.g. an Automobile class might implement the Recyclable interface, which could apply to many otherwise totally unrelated objects

A third party class must be rewritten to extend only from the abstract class.
An abstract class defines the core identity of its descendants. If you defined a Dog abstract class then Damamation descendants are Dogs, they are not merely dogable. Implemented interfaces enumerate the general things a class can do, not the things a class is. If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.

Adding functionality

If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method.

1.C# and OO Concepts


In which Scenario you will go for Interface or Abstract Class? Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes. Even though class inheritance allows your classes to inherit implementation from a base class, it also forces you to make most of your design decisions when the class is first published. Abstract classes are useful when creating components because they allow you specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class is needed. They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code. Why an error is thrown while a method is written without a return type. What is the expected error? not all code paths return a value.

1.C# and OO Concepts


Overload/Override
Overloading is a feature found in object oriented programming languages that allows the creation of several functions with the same name which differ from each other in terms of the type of the input and the type of the output of the function. An example of this would be a square function which takes a number and returns the square of that number. In this case, it is often necessary to create different functions for integer and floating point numbers. overriding, in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides (replaces) the implementation in the superclass. A subclass can give its own definition of methods which also happen to have the same signature as the method in its superclass. This means that the subclass's method has the same name and parameter list as the superclass's overridden method. Constraints on the similarity of return type vary from language to language, as some languages support covariance on return types.

1.C# and OO Concepts


What is operator overloading in .Net? It provides a way to define and use operators such as +, -, and / for userdefined classes or structs. It allows us to define/redefine the way operators work with our classes and structs. This allows programmers to make their custom types look and feel like simple types such as int and string. VB.NET till now does not support operator overloading. Operator overloading is done by using the Operator keyword How to call superclasss member in subclass? Using base keyword

1.C# and OO Concepts


Virtual/new
Virtual functions implement the concept of polymorphism, except that you use the override keyword with the virtual function implementation in the child class. The parent class uses the same virtual keyword. Every class that overrides the virtual method will use the override keyword. Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier. You can define in a child class a new version of a function, hiding the one which is in base class. A new keyword is used to define a new version. What does virtual keyword mean ? That method and property can be overridden. What are Sealed Classes in C#? The sealed modifier is used to prevent derivation from a class. A compile-time error occurs if a sealed class is specified as the base class of another class. (A sealed class cannot also be an abstract class

1.C# and OO Concepts


Value/Ref-Type Value/Ref-Type : Value types directly contain their data are either allocated on the stack or allocated in-line in a structure. Reference types store a reference to the value's memory address, and are allocated on the heap. Reference types can be selfdescribing types, pointer types, or interface types. Variables that are value types each have their own copy of the data, and therefore operations on one variable do not affect other variables. Variables that are reference types can refer to the same object; therefore, operations on one variable can affect the same object referred to by another variable. All types derive from the System. Object base type. What is boxing? Boxing permits any value type to be implicitly converted to type object or to any interface type implemented by value type. Boxing is process in which a object instances created and copying value types value in to that instance.

What is unboxing? Unboxing is vice versa of boxing operation where the value is copied from the instance in to appropriate storage location.

[continued.]
Give one example for boxing and unboxing Below is sample code of boxing and unboxing where integer data type is converted in to object and then vice versa. Dim x As Integer Dim y As Object x = 10 boxing process y = x unboxing process x = y

StringBuilder Represents a mutable string of characters. This class cannot be inherited. This class represents a string-like object whose value is a mutable sequence of characters. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters
What is the difference between System.String and System.Stringbuilder Classes? System.String is immutable; System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.

1.C# and OO Concepts


Generics ** Generics are a new feature in version 2.0 of the C# language and the common language runtime (CLR). Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. Generics Overview Use generic types to maximize code reuse, type safety, and performance. The most common use of generics is to create collection classes. The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. These should be used whenever possible in place of classes such as ArrayList in the System.Collections namespace. You can create your own generic interfaces, classes, methods, events and delegates. Generic classes may be constrained to enable access to methods on particular data types. Information on the types used in a generic data type may be obtained at run-time by means of reflection.

[continued.]
What are Generic type parameters? In a generic type or method definition, a type parameters is a placeholder for a specific type that a client specifies when they instantiate a variable of the generic type. GenericList<float> list1 = new GenericList<float>(); GenericList<ExampleClass> list2 = new GenericList<ExampleClass>(); GenericList<ExampleStruct> list3 = new GenericList<ExampleStruct>();

In each of these instances of GenericList<T>, every occurrence of T in the class will be substituted at run time with the type argument. By means of this substitution, we have created three separate type-safe and efficient objects using a single class definition.
What are Generic classes? Generic classes encapsulate operations that are not specific to a particular data type. The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees and so on where operations such as adding and removing items from the collection are performed in much the same way regardless of the type of data being stored.

1.C# and OO Concepts


Unsafe, Managed/Unmanaged **
Unsafe :The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.You can use the unsafe modifier in the declaration of a type or a member. The entire textual extent of the type or member is therefore considered an unsafe context

1.C# and OO Concepts


Managed/Unmanaged : Managed code runs inside the environment of CLR i.e. .NET runtime. In short all IL are managed 7code. But if you are using some third party software example VB6 or VC++ component they are unmanaged code as .NET runtime (CLR) does not have control over the source code execution of the language.

Managed Code?
Managed code runs inside the environment of CLR i.e. .Net run-time. In short, all IL are managed code. However, if you are using some third party software example VB6 or VC++ component they are unmanaged code, as .NET runtime (CLR) does not control over the source code execution of these languages.

1.C# and OO Concepts


Managed Code is what Visual Basic .NET and C# compilers create. It compiles to Intermediate Language (IL), not to machine code that could run directly on your computer. The IL is kept in a file called an assembly, along with metadata that describes the classes, methods, and attributes (such as security requirements) of the code you've created. This assembly is the one-stop-shopping unit of deployment in the .NET world. You copy it to another server to deploy the assembly thereand often that copying is the only step required in the deployment. Managed code runs in the Common Language Runtime. The runtime offers a wide variety of services to your running code. In the usual course of events, it first loads and verifies the assembly to make sure the IL is okay. Then, just in time, as methods are called, the runtime arranges for them to be compiled to machine code suitable for the machine the assembly is running on, and caches this machine code to be used the next time the method is called. (This is called Just In Time, or JIT compiling, or often just Jitting.) As the assembly runs, the runtime continues to provide services such as security, memory management, threading, and the like. The application is managed by the runtime. Visual Basic .NET and C# can produce only managed code. If you're working with those applications, you are making managed code. Visual C++ .NET can produce managed code if you like: When you create a project, select one of the application types whose name starts with .Managed., such as .Managed C++ application..

[continued.]
Unmanaged code is what you use to make before Visual Studio .NET 2002 was released. Visual Basic 6, Visual C++ 6, heck, even that 15-year old C compiler you may still have kicking around on your hard drive all produced unmanaged code. It compiled directly to machine code that ran on the machine where you compiled itand on other machines as long as they had the same chip, or nearly the same. It didn't get services such as security or memory management from an invisible runtime; it got them from the operating system. And importantly, it got them from the operating system explicitly, by asking for them, usually by calling an API provided in the Windows SDK. More recent unmanaged applications got operating system services through COM calls. Unlike the other Microsoft languages in Visual Studio, Visual C++ can create unmanaged applications. When you create a project and select an application type whose name starts with MFC, ATL, or Win32, you're creating an unmanaged application. This can lead to some confusion: When you create a .Managed C++ application., the build product is an assembly of IL with an .exe extension. When you create an MFC application, the build product is a Windows executable file of native code, also with an .exe extension. The internal layout of the two files is utterly different. You can use the Intermediate Language Disassembler, ildasm, to look inside an assembly and see the metadata and IL. Try pointing ildasm at an unmanaged exe and you'll be told it has no valid CLR (Common Language Runtime) header and can't be disassembledSame extension, completely different files.

1.C# and OO Concepts


How .Net Flow works like MSIL and all. When compiling to managed code, the compiler translates your source code into Microsoft intermediate language (MSIL), which is a CPU-independent set of instructions that can be efficiently converted to native code. MSIL includes instructions for loading, storing, initializing, and calling methods on objects, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception handling, and other operations. Microsoft intermediate language (MSIL) is a language used as the output of a number of compilers and as the input to a just-intime (JIT) compiler. The common language runtime includes a JIT compiler for converting MSIL to native code

What are the steps that GC follows for collection? The basic algorithm used by the garbage collector is quite simple: Mark all managed memory as garbage Look for used memory blocks, and mark them as valid Discard all unused memory blocks Compact the heap

Questions and Comments

You might also like