Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
// Namespace Declaration
using System;
csc.exe 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.
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.
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.
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.
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);
}
}
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());
}
}