C# Basic Subjective Questions with Answer
C# Basic Subjective Questions with Answer
.NET is tiered, modular, and hierarchal. Each tier of the .NET Framework is a layer of
abstraction. .NET languages are the top tier and the most abstracted level. The common
language runtime is the bottom tier, the least abstracted, and closest to the native
environment. This is important since the common language runtime works closely with
the operating environment to manage .NET applications. The .NET Framework is
partitioned into modules, each with its own distinct responsibility. Finally, since higher
tiers request services only from the lower tiers, .NET is hierarchal. The architectural
layout of the .NET Framework is illustrated in Figure
.NET abstracts lower-level services, while retaining most of their flexibility. This is
important to C-based programmers, who shudder at the limitations presented in Visual
Basic 6 and earlier.
Let us examine each tier of the .NET Framework as it relates to a managed environment,
language interoperability, and abstraction of lower-level services.
It seems unusual I know, but I will explain how it's done. Let's take a look at the very first
example that explains how to use a delegate.
Copy the following code into your VS.NET class file and run the project.
using System;
namespace Delegates
{
public delegate int DelegateToMethod(int x, int y);
public class Math
{
public static int Add(int first, int second)
{
return first + second;
}
}
}
We have defined a new delegate type using the statement
You are used to defining a new class using the class keyword, then an identifier followed
by {, then implementation in the form of methods, properties and fields followed by }.
The case is different with delegates. When we define a new delegate type (like
DelegateToMethod) the C# compiler generates a class called DelegateToMethod that
derives the System.MultipcastDelegate as follows:
Reflection in .NET…?
Refelction is the mechanism of discovering class information solely at run
time.Wondering where it would be useful? Imagine,you are in visual studio IDE
(Integrated devolopment environment) and as you type "object." you would see all the
methods,properties and events associated with that object.An other example would be an
Object browser.The code attached here,loads all assemblies and displays each class,its
contructors, methods, properties and events in tree view.The form also hase a command
botton (labeled More Reflection) in whose click event I create an object of MyClass1
class based on the name of the class and invoke its methods during run time.Here is an
explanation of the code with information on various classes and methods used.
System.Reflection.Assembly :
It defines an assembly which is a reusable,versionable and self describing building block
of a Common Language Run time application.I will discuss here only the functions that I
have used in my code.
objAssembly=System.Reflection.Assembly.LoadFrom(str);
LoadFrom function takes the path of the assembly to load and loads it and returns that
Assembly.
After loading the assembly we want to get all the types in that assembly.This class
provides a function GetTypes() (It returns an array of System.Type objects in that
assembly) which will do that for us and that line for the code is.
arOfTypes=objAssembly.GetTypes();
POLYMORPHISM in C# …?
Through inheritance, a class can be used as more than one type; it can be used as its own
type, any base types, or any interface type if it implements interfaces. This is called
polymorphism. In C#, every type is polymorphic. Types can be used as their own type or
as a Object instance, because any type automatically treats Object as a base type.
Polymorphism is important not only to the derived classes, but to the base classes as well.
Anyone using the base class could, in fact, be using an object of the derived class that has
been cast to the base class type. Designers of a base class can anticipate the aspects of
their base class that are likely to change for a derived type. For example, a base class for
cars might contain behavior that is subject to change when the car in question is a
minivan or a convertible. A base class can mark those class members as virtual, allowing
derived classes representing convertibles and minivans to override that behavior.
A Base Class With a Virtual Method: GenericObject.cs
using System;
public class GenericObject
{
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing object.");
}
}
Derived Class With Override Methods: Line.cs
using System;
public class Line : GenericObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
The class Line inherit the GenericObject class. Line class has a Draw() method and
Draw() method has an override modifier. The override modifier allows a method to
override the virtual method of its base class at run-time. The override will happen only if
the class is referenced through a base class reference. Overriding methods must have the
same signature, name and parameters, as the virtual base class method it is overriding.
using System;
public class Demo
{
public static int Main( )
{
GenericObject[] dObj = new GenericObject[2];
dObj[0] = new Line();
dObj[1] = new GenericObject();
foreach (GenericObject drawObj in dObj)
{
drawObj.Draw();
}
return 0;
}
}
Because of polymorphism in the above case, the run-time type of each object is invoked.
The type of the reference object from the dObj array is a GenericObject. The derived
class overrides the virtual Draw() method of the GenericObject class. This calls the
overriden Draw() methods of the derived “Line” class execute when the Draw() method
is called using the GenericObject base class .
ENCAPSULATION in C# …?
Encapsulation is a mechanism for hiding instance variables and irrelevant methods of a
class from other objects. Only methods, which are necessary to use an object of the class,
are exposed.
1. Using properties
C++ programmers try to keep member variables private. This data hiding promotes
encapsulation and allows you to change your implementation of the class without
breaking the interface your clients rely on.
To the user, a property looks like a member variable, but to the implementor of the class
it looks like a method. Properties allow you total encapsulation and data hiding while
giving your users easy access to the members.
You can provide a Student class with an Age property to allow users to get and set the
student's age member.
You can create a read-only property for his PerformanceRank by implementing the Get
and not the Set accessor.
You can get Venkatesh's age through the property and then you can use that property to
set the age. You can access the PerformanceRank property to obtain the value, but not to
set it
If you decide later to retrieve the student’s age from a database, you need change only the
accessor implementation; the client will not be affected.
Let us see an example of an Employee class. To manipulate the data in that class (String
empname) we define an accessor (get method) and mutator (set method).
using system;
.......
// Accessor Method
{
return empname;
// Mutator Method
empname=tempname;
Like the above way we can protect the private data from the outside world. Here we use
two separate methods to assign/access and get/retrieve the required data.
e.SetEmpname("VENKATESH");
return 0;
}
What is meant by CLR …?
CLR is stands for Common Language Runtime.It is a part of the .Net Framework.all .net
language languages run under the control of the .net framwork.
and CLR follows no of the features
1.Garbage Collector
2.Code Access Security.
3.Type Safety
4.IL Optimizers to Native code. etc……
.NET Assembly…?
65. Assemblies are the building blocks of .NET Framework applications; they form the
fundamental unit of deployment, version control, reuse, activation scoping, and security
permissions. An assembly is a collection of types and resources that are built to work
together and form a logical unit of functionality. An assembly provides the common
language runtime with the information it needs to be aware of type implementations. To
the runtime, a type does not exist outside the context of an assembly.
Assemblies are a fundamental part of programming with the .NET Framework. An
assembly performs the following functions:
66.
* It contains code that the common language runtime executes. Microsoft
intermediate language (MSIL) code in a portable executable (PE) file will not be
executed if it does not have an associated assembly manifest. Note that each assembly
can have only one entry point (that is, DllMain, WinMain, or Main).
* It forms a security boundary. An assembly is the unit at which permissions are
requested and granted.
* It forms a type boundary. Every type's identity includes the name of the assembly
in which it resides. A type called MyType loaded in the scope of one assembly is not the
same as a type called MyType loaded in the scope of another assembly.
* It forms a reference scope boundary. The assembly's manifest contains assembly
metadata that is used for resolving types and satisfying resource requests. It specifies the
types and resources that are exposed outside the assembly. The manifest also enumerates
other assemblies on which it depends.
* It forms a version boundary. The assembly is the smallest versionable unit in the
common language runtime; all types and resources in the same assembly are versioned as
a unit. The assembly's manifest describes the version dependencies you specify for any
dependent assemblies.
* It forms a deployment unit. When an application starts, only the assemblies that
the application initially calls must be present. Other assemblies, such as localization
resources or assemblies containing utility classes, can be retrieved on demand. This
allows applications to be kept simple and thin when first downloaded.
* It is the unit at which side-by-side execution is supported.
Assemblies can be static or dynamic. Static assemblies can include .NET Framework
types (interfaces and classes), as well as resources for the assembly (bitmaps, JPEG files,
resource files, and so on). Static assemblies are stored on disk in PE files. You can also
use the .NET Framework to create dynamic assemblies, which are run directly from
memory and are not saved to disk before execution. You can save dynamic assemblies to
disk after they have executed.
There are several ways to create assemblies. You can use development tools, such as
Visual Studio .NET, that you have used in the past to create .dll or .exe files. You can use
tools provided in the .NET Framework SDK to create assemblies with modules created in
other development environments. You can also use common language runtime APIs,
such as Reflection.Emit, to create dynamic assemblies.