Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

C# Chapter 2

Q. Define Managed and Unmanaged Code.
OR
Q. What is Managed and Unmanaged Code?
OR
Q. What are the differences between Managed and Unmanaged Code?


Ans.  The code which is executes under the management of Common Language Runtime is known as Managed code. The code of VB.NET and C# is the example of Managed code. The compiler of VB.NET and C# generates the MSIL (Microsoft Intermediate Language) which is also known as IL (Intermediate Language). IL not execute directly on to machine, firstly it’s goes to CLR where CLR convert IL into Native Code (Executable Code). The IL is kept in a file called an assembly, along with metadata that describes the classes, methods, and attributes (such as security requirements) of the code you've created. Managed code runs in the Common Language Runtime. The CLR offers a wide variety of services to your running code. Firstly it loads and verifies the assembly to make sure the IL is okay. Then, just in time, as methods (functions) are called, the runtime arranges for them to be compiled to machine code suitable for the machine the assembly is running on, and caches this machine code to be used the next time. This method is known as JIT (Just In Time), or JIT compiling, or often just Jitting.

As the assembly runs, the runtime continues to provide services such as security, memory management, threading, and the like. The application is managed by the runtime.
Visual Basic .NET and C# can produce only managed code. If you're working with those applications, you are making managed code. Visual C++ .NET can produce managed code if you like. When you create a project, select one of the application types whose name starts with .Managed., such as .Managed C++ application.


The code which is not executes under the management of CLR (Common Language Runtime) is known as Unmanaged code. C, VB6.0, VC++ generate the Unmanaged Code. The compilers of these languages directly generate the machine code which directly runs on the machine or these compilers compile the source code directly to machine code that run on the machine where you compiled it—and 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.



Q. Explain the evolution of C#.

Ans. The evolution of C# is associated with the evolution .NET Framework. Bill Gates the founder of Microsoft want to develop a software platform which can enable the users to get information anytime and anywhere, using a natural interface. The platform should be a collection of readily available web services that can be distributed and accessed via standard Internet protocols. He wants to make the web both programmable and intelligent. Microsoft has a vision ‘Software as a Service’, to full fill this requirement Microsoft launched a new generation platform called .NET. The MPDC (Microsoft Professional Developers Conference) in September 2000, officially announced .NET and C#. C# has been particularly designed to build software components for .NET and it supports key features of .NET natively.


C# is derived from C and C++. C# has the features of C, C++, Vb and Java. C# has the features of java such as grouping of classes, interfaces and implementation together in one file so that programmers can edit the code more easily. Like Java C# use the references to handle the objects. The using of tools and controls in C# is very similar of VB, we can design the form, dragging the controls from toolbox and drop them on to form, coding of controls on there events is very similar of VB. From year 2000 to till.

The first release of C# (C# 1.0) was all about building a new language for managed code that appealed, mostly, to C++ and Java programmers.

The second release (C# 2.0) was mostly about adding what wasn’t time to built into the 1.0 release. The main feature for this release was Generics.

The third release (C# 3.0) was all about reducing the impedance mismatch between general purpose programming languages and databases. To achieve this goal, several functional programming features were added to the language and LINQ was born.


The fourth release C# 4.0 was on dynamic programming. Not just dynamic types but being able to talk with anything that isn’t a .NET class. C# 4.0 is co-variance and contra-variance for generic interfaces and delegates.

Q. Write and explain a welcome program in C#.
OR
Q. Write the first program in C#.
OR
Q. Describe the structure of typical C# program.

Ans. C# programs can consist of one or more files. Each file can contain zero or more namespaces. A namespace can contain types such as classes, structs, interfaces, enumerations, and delegates, in addition to other namespaces. The first program of C# is given below:-


using System;

namespace TheKhokhar
{
class Hello
{
   static void Main()
 {
      Console.WriteLine("Welcome in C#");
   }
}
}

using –  It is a directive which is use for to create an alias for a namespace (a using alias), or it is use for import an predefine namespace into project.
Permit the use of types in a namespace, such that, you do not have to qualify the use of a type in that namespace (a using directive).
System – System is a predefine namespace of .Net Class Libraries.
namespace TheKhokhar– TheKhokhar is the name of namespace which is crated by programmer.
class Hello – This line of code create a class which name is Hello.
static void Main( ) – static is a keyword , void is the return type of Main( ) function. Every C# program must have one Main( ) function, which is the entry point of program. Program is always going to execute from Main( ) function.
Console – This is a class of System namespace.
WriteLine – This is an output function of Console class. This function automatically insert the ‘\n’ at the end of line.



Q. What is namespace?

Ans. namespace are the logical component that consist the classes, interfaces, structs, enum, delegates, nested namespaces etc. namespace contains classes that support compilation and code generation using the C# language.
OR
Namespaces are C# program elements designed to help you organize your programs. They also provide assistance in avoiding name clashes between two sets of code.
e.g.
namespace SampleNamespace
{
    interface SampleInterface
{
..
}
    class SampleClass
{
..
}
    struct SampleStruct
{
..
}
    enum SampleEnum
{
a,b
}
    delegate void SampleDelegate(int i);
    namespace SampleNamespace.Nested
    {
       class SampleClass2{}
    }
}


Namespaces allow you to create a system to organize your code. A good way to organize your namespaces is via a hierarchical system. You put the more general names at the top of the hierarchy and get more specific as you go down. This hierarchical system can be represented by nested namespaces. e.g.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            abtry.abc obj = new abtry.abc();
            obj.show();
        }
    }
    namespace abtry
    {
        class abc
        {
            public void show()
            {
                Console.WriteLine("Welcome at C#");
            }
        }
    }
}

Whether or not you explicitly declare a namespace in a C# source file, the compiler adds a default namespace. This unnamed namespace, sometimes called the global namespace, is present in every file. Any identifier in the global namespace is available for use in a named namespace.


Q. Define the class and object in C#.

Ans. C# is OOP oriented language. OOP stands on Object Oriented Programming. Before the OOP languages, software/programs are build in procedure oriented languages, where programmer face the number of problems. But when OOP languages come in market programmers like to work in OOP languages. Language which support the OOP, code is written in the classes. Description of class is given below:-

C# classes

A C# class considered being as a primary building block of language, means if you want to create a program in C# you must have to create a class first. We use classes as a template to put the properties, functionalities or behaviors in one building block for some group of objects and after that we use that template/class to create the objects we need. To explain the behavior/function and properties we take an example given below:-

Suppose person is a class

1. The properties of any normal person are: Hair Color, Age, Height, Weight, Eyes Color etc.

2- The functionalities or behaviors of any normal person are: Drink water, Eat, Go to the work, Reading, Taking etc.

A class is a logical entity which is declared with ‘class’ keyword. A class contains data (in the form of variables and properties) and behaviors (in the form of methods/functions to process these data). When we declare a variable in a class we call it member variables or instance variables. The name instance come from the fact that when we create an object we instance a class to create that object so instance of a class means object of that class and instance variable means variable that exists in that class.

C# objects
An object is the physical entity. We have to create an object of class before accessing the members of class. We create the object of class because variables declared in a class store the data for each instance/object. Means that when we create an object of class the object will allocate a memory location to store the data of its variables. An object is created with ‘new’( ‘new’ is a reserve operator in C#). After creating an object of class we can access the ‘non-static’ (static members are direct access via class name) members of class. Each object takes the different space into memory.
Example:-


No comments:

Post a Comment