Sei sulla pagina 1di 6

1. Explain the basic elements of a C# program.

Illustrate every
aspect completely through a simple C# program structure

Ans: There are basic elements that all C# executable programs have. After
reviewing the following code we will see the basic concepts that will follow
for all C# programs.

A Simple HelloWorld Program: HelloWorld.cs

// Namespace Declaration
using System;

// Program start class


class HelloWorld
{
// Main begins program execution.
static void Main()
{
// Write to console
Console.WriteLine("HelloWorld");
}
}

The program shown above has 4 primary elements, a namespace


declaration, a class, a Main method, and a program statement. It can be
compiled with the following command line:

csc.exe HelloWorld.cs

This produces a file named HelloWorld.exe, which can then be executed.


Other programs can be compiled similarly by substituting their file name
instead of HelloWorld.cs.

The first thing you should be aware of is that C# is case-sensitive. The word
"Main" is not the same as its lower case spelling, "main". They are different
identifiers. The namespace declaration, using System;, indicates that you are
referencing the System namespace. Namespaces contain groups of code
that can be called upon by C# programs. With the using System; declaration,
you are telling your program that it can reference the code in the System
namespace without pre-pending the word System to every reference.

The class declaration, class HelloWorld, contains the data and method
definitions that your program uses to execute. A class is one of a few
different types of elements your program can use to describe objects, such
as structs, interfaces, delegates, and enums. This particular class has no
data, but it does have one method. This method defines the behavior of this
class. The one method within the HelloWorld class tells what this class will do
when executed. The method name, Main, is reserved for the starting point of
a program. Main is often called the "entry point" and if you ever receive a
compiler error message saying that it can't find the entry point, it means that
you tried to compile an executable program without a Main method.

A static modifier precedes the word Main, meaning that this method works in
this specific class only, rather than an instance of the class. This is
necessary, because when a program begins, no object instances exist.

Every method must have a return type. In this case it is void, which means
that Main does not return a value. Every method also has a parameter list
following its name with zero or more parameters between parentheses. For
simplicity, we did not add parameters to Main.

The Main method specifies its behavior with the Console.WriteLine(...)


statement. Console is a class in the System namespace. WriteLine(...) is a
method in the Console class. We use the ".", dot, operator to separate
subordinate program elements.

Observe that comments are marked with "//". These are single line
comments, meaning that they are valid until the end-of-line. If you wish to
span multiple lines with a comment, begin with "/*" and end with "*/".
Everything in between is part of the comment. Comments are ignored when
your program compiles.

All statements end with a ";", semi-colon. Classes and methods begin with
"{", left curly brace, and end with a "}", right curly brace. Any statements
within and including "{" and "}" define a block. Blocks define scope (or
lifetime and visibility) of program elements.

2. How the .NET framework overcome the problems of COM


(Component Object Model).

Ans: NET technology is a third-generation component model. This provides a


new level of inter-operability compared to COM technology. COM provides a
standard binary mechanism for inter-module communication. This
mechanism is replaced by an intermediate language called Microsoft
Intermedia Language (MSIL) or simply IL in the .NET technology. Various
.NET-Ianguage compilers enforce inter- operability by compiling code into IL,
which is automatically compatible with other IL modules. An inherent
characteristic of IL code is metadata. Metadata is data about data and
describes its characteristics, including data types and locations. IL allows for
true cross-language integration. In addition to IL, .NET includes a host of
other technologies and tools that will enable us develop and implement Web-
based applications easily.

3. What is the dll hell problem plagering language system?


How .NET address this problem?

Ans: The Problem

Previously, before .NET, this used to be a major issue. "DLL Hell" refers to the
set of problems caused when multiple applications attempt to share a
common component like a dynamic link library (DLL) or a Component Object
Model (COM) class. In the most typical case, one application will install a new
version of the shared component that is not backward compatible with the
version already on the machine. Although the application that has just been
installed works well, existing applications that depended on a previous
version of the shared component might no longer work. In some cases, the
cause of the problem is even more subtle. In many cases there is a
significant delay before a user discovers that an application has stopped
working. As a result, it is often difficult to remember when a change was
made to the machine that could have affected the application. A user may
remember installing something a week ago, but there is no obvious
correlation between that installation and the behavior they are now seeing.
The reason for these issues is that version information about the different
components of an application aren't recorded or enforced by the system.
Also, changes made to the system on behalf of one application will typically
affect all applications on the machine.

How .NET addresses DLL Hell?

Microsoft .Net 1.1, which is integral to the Windows Server 2003 operating
systems, supports strong binding. Strong binding means an application or
component can bind to a specific version of another component, so you can
reuse components or use them in isolation.
.Net 1.1 will provide Windows Server 2003 operating systems with a Global
Assembly Cache. This Cache is a repository for all the .Net components that
are shared globally on a particular machine. When a .Net component is
installed onto the machine, the Global Assembly Cache looks at its version,
its public key, its language information and creates a strong name for the
component. The component is then registered in the repository and indexed
by its strong name, so there is no confusion between different versions of the
same component, or DLL.

Windows 2003 Server also uses rules to make sure that an application finds
the right component and version. The system will first look for a local version
of the component, and will then look in the cache to find an exact match for
the strong name of the required component. Failing that, the system will use
heuristics to find the next best component, but by default an application will
always run against the component that it was built and tested against.
Administrators will be able to override these rules for exceptional cases.

Another feature of Windows Server 2003 is that .Net components will have
no registration policy. This means it will be easy to take a .Net component on
server and copy to another server. Microsoft is calling the feature xcopy
deploy, after a command used in DOS to copy capability files, directories and
even whole drives from one destination to another. It means you can copy
applications instead of reinstalling them and the whole process becomes
much simpler.

Also, .NET Framework 1.1 introduced something called side-by-side


execution. Side by side is the ability to install and run multiple versions of
the same component on the machine concurrently at the same time without
interfering with each other. With components that support side-by-side,
authors aren't necessarily tied to maintaining strict backward compatibility
because different applications are free to use different versions of a shared
component.

4. How you will convert a tail recursive program to an iterative


program?

Ans: Tail recursion is a specialized form of the linear recursion where the
last operation of the function happens to be a recursive call.

Recursive
using System;
class Program
{
static long fact(int n)
{
if (n == 0) return 1;
return n * fact(n - 1);
}
static void Main(string[] args)
{
Console.WriteLine(fact(5));
}
}

Iterative
using System;
class Program
{
static void Main(string[] args)
{
int result = 1,n=5;
for (int i = 1; i <= n; ++i)
{
result *= i;
}
Console.WriteLine(result);
}
}

5. In which condition do you typically override Get Hash Code


method? How exactly overriding this method helps us?

Ans: Use the override modifier to modify a method, a property, an indexer,


or an event. An override method provides a new implementation of a
member inherited from a base class. The method overridden by an override
declaration is known as the overridden base method. The overridden base
method must have the same signature as the override method.

You cannot override a non-virtual or static method. The overridden base


method must be virtual, abstract, or override.

An override declaration cannot change the accessibility of the virtual


method. Both the override method and the virtual method must have the
same access level modifier.
You cannot use the following modifiers to modify an override method:

new static virtual abstract

An overriding property declaration must specify the exact same access


modifier, type, and name as the inherited property, and the overridden
property must be virtual, abstract, or override.

Example

using System;
class TestClass
{
public class square
{
public double x;
public Square(double x)
{
this.x = x;
}
public virtual double Area()
{
return x*x;
}
}
class Cube: Square
{
// Constructor:
public Cube(double x): base(x)
{
}
public override double Area()
{
return (6*(base.Area()));
}
}
public static void Main()
{
double x = 5.2;
Square s = new Square(x);
Square c = new Cube(x);
Console.WriteLine("Area of Square = {0:F2}", s.Area());
Console.WriteLine("Area of Cube = {0:F2}", c.Area());
}
}

Potrebbero piacerti anche