Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

C# Basic Subjective Questions with Answer

.NET architecture is a tiered, modular, and hierarchical framework where the common language runtime (CLR) is the lowest tier, managing application execution and providing essential services. Delegates in .NET are objects that reference methods, allowing for event handling and method invocation, while reflection enables runtime discovery of class information. Key concepts in C# include polymorphism, encapsulation, and exception handling, all of which enhance code flexibility, security, and robustness.

Uploaded by

alpeshconnect
Copyright
© Public Domain
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C# Basic Subjective Questions with Answer

.NET architecture is a tiered, modular, and hierarchical framework where the common language runtime (CLR) is the lowest tier, managing application execution and providing essential services. Delegates in .NET are objects that reference methods, allowing for event handling and method invocation, while reflection enables runtime discovery of class information. Key concepts in C# include polymorphism, encapsulation, and exception handling, all of which enhance code flexibility, security, and robustness.

Uploaded by

alpeshconnect
Copyright
© Public Domain
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Explain The .NET Architecture…?

.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 Framework is a managed environment. The common language runtime monitors


the execution of .NET applications and provides essential services. It manages memory,
handles exceptions, ensures that applications are well-behaved, and much more.
Language interoperability is one goal of .NET. .NET languages share a common runtime
(the common language runtime, a common class library), the Framework Class Library
(FCL), a common component model, and common types. In .NET, the programming
language is a lifestyle choice. Except for subtle differences, C#, VB.NET, or JScript.NET
offer a similar experience.

.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.

Explain Delegate in event creation with example….?


A delegate is an object that refers to a static method or an instance method. A delegate is
an object that is created to refer to a static method or an instance method, and then used
to call this method. To start off, you create a new delegate type in a different way than
you create any other class. You use the delegate keyword as in the following statement.

public delegate int DelegateToMethod(int x, int y);

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.

The First Delegate Example

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;
}

public static int Multiply(int first, int second)


{
return first * second;
}
public static int Divide(int first, int second)
{
return first / second;
}
}

public class DelegateApp


{
public static void Main()
{
DelegateToMethod aDelegate = new DelegateToMethod(Math.Add);
DelegateToMethod mDelegate = new DelegateToMethod(Math.Multiply);
DelegateToMethod dDelegate = new DelegateToMethod(Math.Divide);
Console.WriteLine("Calling the method Math.Add() through the aDelegate object");
Console.WriteLine(aDelegate(5,5));
Console.WriteLine("Calling the method Math.Multiply() through the mDelegate
object");
Console.WriteLine(mDelegate(5,5));
Console.WriteLine("Calling the method Math.Divide() through the dDelegate
object");
Console.WriteLine(dDelegate(5,5));
Console.ReadLine();
}

}
}
We have defined a new delegate type using the statement

public delegate int DelegateToMethod(int x, int y);

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:

public sealed class DelegateToMethod : System.MulticastDelegate


{
public DelegateToMethod(object target, int method);
public virtual void Invoke(int x, int y);
public virtual IAsyncResult BeginInvoke(int x, int y,
AsyncCallback callback, object obj);
public virtual void EndInvoke(IAsyncResult result);
}
The Constructor method of this class takes two arguments. The first is an object reference
of the type that defined the instance method that the delegate refers to, and the second is
an int value of the function pointer to the method that the delegate encapsulates.

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.

Program Implementing Polymorphism: Demo.cs

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.

Encapsulation can be implemented using the following methods in C#

1. Using properties

2. Using accessor Mutator methods

ENCAPSULATION 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.

public int Age


{
get
{
return age;
}
set
{
age = value;
}
}
The keyword value is implicitly available to the property. If you write
Venkatesh.Age = 17;
the compiler will pass in the value 17 as value.

You can create a read-only property for his PerformanceRank by implementing the Get
and not the Set accessor.

public int PerformanceRank


{
get
{
return performanceRank;
}
}

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.

ENCAPSULATION USING ACCESSOR AND MUTATOR METHODS:

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;

public class Employee

private string empname;

.......

// Accessor Method

public string GetEmpname()

{
return empname;

// Mutator Method

public void SetEmpname( string tempname)

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.

public static int Main(string[] args)

Employee e= new Employee()

e.SetEmpname("VENKATESH");

Console.WriteLine("The Department is :"+e.GetEmpname());

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……

What is exception handling …?


When an exception occurs, the system searches for the nearest catch clause that can
handle the exception, as determined by the run-time type of the exception. First, the
current method is searched for a lexically enclosing try statement, and the associated
catch clauses of the try statement are considered in order. If that fails, the method that
called the current method is searched for a lexically enclosing try statement that encloses
the point of the call to the current method. This search continues until a catch clause is
found that can handle the current exception, by naming an exception class that is of the
same class, or a base class, of the run-time type of the exception being thrown. A catch
clause that doesn't name an exception class can handle any exception.
Once a matching catch clause is found, the system prepares to transfer control to the first
statement of the catch clause. Before execution of the catch clause begins, the system first
executes, in order, any finally clauses that were associated with try statements more
nested that than the one that caught the exception.
Exceptions that occur during destructor execution are worth special mention. If an
exception occurs during destructor execution, and that exception is not caught, then the
execution of that destructor is terminated and the destructor of the base class (if any) is
called. If there is no base class (as in the case of the object type) or if there is no base
class destructor, then the exception is discarded.

.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.

What are the contents of assembly …?


In general, a static assembly can consist of four elements:
* The assembly manifest, which contains assembly metadata.
* Type metadata.
* Microsoft intermediate language (MSIL) code that implements the types.
* A set of resources.

You might also like