Professional Documents
Culture Documents
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>
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
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.
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.
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.
[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.
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.
[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.
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