Sie sind auf Seite 1von 34

Q.1 Explain Features of .NET ?

Ans: The Features of .net are as follows:


1. SIMPLE
o Pointers are missing in C#.
o Unsafe operations such as direct memory manipulation are not allowed.
o In C# there is no usage of “::”or "->" operators.
o It inherits the features of automatic memory management and garbage
collection.
o Varying ranges of the primitive types like Integer,Floats etc.
o Integer values of 0 and 1 are no longer accepted as Boolean values.
Boolean values are pure true or false values in C# so no more errors of
"="operator and "=="operator.
o "==" is used for comparison operation and "=" is used for assignment
operation.
2. MODERN
o C# has been based according to the current trend and is very powerful
and simple for building interoperable, scable, robust applications.
o C# includes built in support to turn any component into a web service
that can be invoked over the internet from any application running on
any platform.
3. OBJECT ORIENTED
o C# supports Data Encapsulation, inheritance, polymorphism, interfaces.
o (Int, float, double) are not objects in java but C# has introduces
structures(structs) which enable the primitive types to become objects.
Int i=1;
string a=i to string (); //conversion (or) Boxing
4. TYPE SAFE
o In C# we cannot perform unsafe casts like convert double to a boolean.
o Value types (primitive types) are initialized to zeros and reference types
(objects and classes) are initialized to null by the compiler automatically.
o Arrays are zero base indexed and are bound checked.
o Overflow of types can be checked.
5. INTEROPERABILITY
o C# includes native support for the COM and windows based applications.
o Allowing restricted use of native pointers.
o Users no longer have to explicitly implement the unknown and other
COM interfaces, those features are built in.
o C# allows the users to use pointers as unsafe code blocks to manipulate
your old code.
o Components from VB NET and other managed code languages and
directlyt be used in C#.
6. SCALABLE AND UPDATEABLE
o .NET has introduced assemblies which are self describing by means of
their manifest. Manifest establishes the assembly identity, version,
culture and digital signature etc. Assemblies need not to be register
anywhere.
o To scale our application we delete the old files and updating them with
new ones. No registering of dynamic linking library.
o Updating software components is an error prone task. Revisions made to
the code. can effect the existing program C# support versioning in the
language. Native support for interfaces and method overriding enable
complex frame works to be developed and evolved over time.

Q.2 Explain execution of C#.NET?


Ans: Execution of C#.Net Process

The Code Execution Process involves the following two stages:


 Compiler time process.
 Runtime process.
1. Compiler time process
o The .Net framework has one or more language compliers, such as Visual
Basic, C#, Visual C++, JScript, or one of many third-party compilers such
as an Eiffel, Perl, or COBOL compiler.
o Any one of the compilers translate your source code into Microsoft
Intermediate Language (MSIL) code.
o For example, if you are using the C# programming language to develop
an application, when you compile the application, the C# language
compiler will convert your source code into Microsoft Intermediate
Language (MSIL) code.
o In short, VB.NET, C# and other language compilers generate MSIL code.
(In other words, compiling translates your source code into MSIL and
generates the required metadata.)
o Currently "Microsoft Intermediate Language" (MSIL) code is also known
as "Intermediate Language" (IL) Codeor "Common Intermediate
Language" (CIL) Code.

SOURCE CODE -----.NET COMLIPER------> BYTE CODE (MSIL + META


DATA)
2. Runtime process.
o The Common Language Runtime (CLR) includes a JIT compiler for
converting MSIL to native code.
o The JIT Compiler in CLR converts the MSIL code into native machine code
that is then executed by the OS.
o During the runtime of a program the "Just in Time" (JIT) compiler of the
Common Language Runtime (CLR) uses the Metadata and converts
Microsoft Intermediate Language (MSIL) into native code.

BYTE CODE (MSIL + META DATA) ----- Just-In-Time (JIT) compiler------>


NATIVE CODE
Note: When you compile a C# application or any application written in a CLS-
compliant language, the application is compiled into MSIL. This MSIL is then
further compiled into native CPU instructions when the application is executed
for the first time by the CLR. (Actually, only the called functions are compiled
the first time they are invoked.)
 From the source code, the compiler generates Microsoft Intermediate
Language (MSIL) which is further used for the creation of an EXE or DLL.
The CLR processes these at runtime. Thus, compiling is the process of
generating this MSIL.

The way you do it in .Net is as follows:


- Right-click and select Build / Ctrl-Shift-B / Build menu, Build command
- F5 - compile and run the application.
- Ctrl+F5 - compile and run the application without debugging.

Compilation can be done with Debug or Release configuration. The


difference between these two is that in the debug configuration, only an
assembly is generated without optimization. However, in release
complete optimization is performed without debug symbols.
Example: /*WAP to swap two numbers.*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int num1, num2, temp;
Console.Write("\nEnter the First Number : ");
num1 = int.Parse(Console.ReadLine());
Console.Write("\nEnter the Second Number : ");
num2 = int.Parse(Console.ReadLine());
temp = num1;
num1 = num2;
num2 = temp;
Console.Write("\nAfter Swapping : ");
Console.Write("\nFirst Number : " + num1);
Console.Write("\nSecond Number : " + num2);
Console.Read();
}
}
}
Output:
o Choose the F5 key to run the program. A Command Prompt window
appears that contains the line Before Swapping and After Swapping.
o Next, the important parts of this program are examined.

Q.3 Explain inheritance and its type.


Ans: Acquiring (taking) the properties of one class into another class is called
inheritance. Inheritance provides reusability by allowing us to extend an
existing class.
o The reason behind OOP programming is to promote the reusability of
code and to reduce complexity in code and it is possible by using
inheritance.
o The following are the types of inheritance in C#.

The inheritance concept is based on a base class and derived class. Let us see
the definition of a base and derived class.
o Base class - is the class from which features are to be inherited into
another class.
o Derived class - it is the class in which the base class features are
inherited.
1.Single inheritance :
It is the type of inheritance in which there is one base class and one derived
class.

For example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication20
{
class Program
{
static void Main(String[]args)

public class Accountcreditinfo //base class


{
public string Credit()
{
return "balance is credited";
}
}
public class debitinfo : Accountcreditinfo //derived class
{
public string debit()
{
Credit(); //derived class method
return "balance is debited";
}
}

}
}
Note: In the preceding sample program Account credit info is the base class
and debit info is the derived class.
2. Hierarchical inheritance:
This is the type of inheritance in which there are multiple classes derived from
one base class. This type of inheritance is used when there is a requirement of
one class feature that is needed in multiple classes.
For example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication23
{
class Program
{
static void Main(string[] args)
class A //base class
{
public string msg()
{
return "this is A class Method";
}
}
class B : A
{
public string info()
{
msg();
return "this is B class Method";
}
class C : A
{
public string getinfo()
{
msg();
return "this is B class Method";
}
}
}
}
}
Note: In the preceding program one base class is derived in many classes
hence it is a called a Hierarchical Inheritance.
3. Multilevel inheritance:
When one class is derived from another derived class then this type of
inheritance is called multilevel inheritance.
For example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication24
{
class Program
{
static void Main(string[] args)
public class Person
{
public string persondet()
{
return "this is the person class";
}
}
public class Bird : Person
{
public string birddet()
{
persondet();
return "this is the birddet Class";
}
}
public class Animal : Bird
{
public string animaldet()
{
persondet();
birddet();
return "this is the Animal Class";
}
}
}
}
Note: In the preceding program, each class is derived from one class that is
derived from another class hence this type of inheritance is called Multilevel
Inheritance.
 Multiple inheritance using Interfaces:
C# does not support multiple inheritances of classes. To overcome this
problem we can use interfaces, we will see more about interfaces in my next
article in detail.

For example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication25
{
class Program
{
static void Main(string[]args)
public interface IA //ineterface 1
{
string setImgs(string a);
}
public interface IB //Interface 2
{
int getAmount(int Amt);
}
public class ICar : IA, IB //implementatin
{
public int getAmount(int Amt)
{
return 100;
}
public string setImgs(string a)
{
return "this is the car";
}
}
}
}
Note: In the preceding program the ICar class inherits the features of the two
interfaces hence this type of inheritance is called Multiple Inheritance.
 The following are some key points about inheritance:
 C# does not support multiple inheritances of classes, the same thing can
be done using interfaces.
 Private members are not accessed in a derived class when one class is
derived from another.

Q.4 Explain types of operator overloading with examples?


Ans: Operator Overloading:
o The concept of overloading a function can also be applied to operators.
Operator overloading gives the ability to use the same operator to do
various operations.
o It provides additional capabilities to c# operators when they are applied
to user-defined data types. It enables to make user-defined
implementations of various operations where one or both of the
operands are of a user-defined class.
o Only the predefined set of c# operators can be overloaded. To make
operations on a user-defined data type is not as simple as the operations
on a built-in data type.
o To use operators with user-defined data types, they need to be
overloaded according to a programmer’s requirement.
o An operator can be overloaded by defining a function to it. The function
of the operator is declared by using the operator keyword.
Syntax:
Access specifier classname operator operator symbol (parameter)
{
//code
}
The following table describes the overloading ability of the various operators
available in C#:

Unary operator overloading:


The return type can be of any type except void for unary operators like !, ~, +
and dot (.) but the return type must be the type of ‘Type’ for – and ++
operators and must be a bool type for true as well as false operators. But do
remember that the true and false operators can be overloaded as pairs only.
The compilation error arises if a class declares one of these operators without
declaring the other.
The following syntax shows the use of Unary operator –
operator (object);
Here, operator is a symbol that denotes a unary operator.
operator a;
Example: /*Write a program to illustrate the unary operator overloading*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Calculator
{
class Calculator
{
public int number1, number2;
public Calculator(int num1, int num2)
{
number1 = num1;
number2 = num2;
}
public static Calculator operator -(Calculator c1)
{
c1.number1 = -c1.number1;
c1.number2 = -c1.number2;
return c1;
}
public void Print()
{
Console.WriteLine("Number1 = " + number1);
Console.WriteLine("Number2 = " + number2);
}
}
class EntryPoint
{
static void Main(String[] args)
{
Calculator calc = new Calculator(15, -25);
calc = -calc;
calc.Print();
}
}
}

Output:
Binary Operators Overloading:
Binary Operators will work with two Operands. Examples of binary operators
include the Arithmetic Operators (+, -, *, /, %), Arithmetic Assignment
operators (+=, -+, *=, /+, %=) and Relational Operators etc. Overloading a
binary operator is similar to overloading a unary operator, except that a binary
operator requires an additional parameter.
Syntax :
Operator operator (object1) (object2);
Here, second "operator" is a symbol that
denotes a binary operator.
operator + (a, b);
Example :
Input : 200, 40
Output : 240
Input : 300, 20
Output : 320
/* program to illustrate the Binary Operator Overloading*/
using System;
namespace BinaryOverload
{
class Calculator
{
public int number = 0;
public Calculator() { }
public Calculator(int n)
{
number = n;
}
public static Calculator operator +(Calculator Calc1,Calculator Calc2)
{
Calculator Calc3 = new Calculator(0);
Calc3.number = Calc2.number + Calc1.number;
return Calc3;
}
public void display()
{
Console.WriteLine("{0}", number);
}
}
class CalNum
{
static void Main(string[] args)
{
Calculator num1 = new Calculator(350);
Calculator num2 = new Calculator(50);
Calculator num3 = new Calculator();
num3 = num1 + num2;
num1.display();
num2.display();
num3.display();
}
}
}
Output:
Benefits of Operator Overloading:
Operator Overloading provides additional capabilities to C# operators when
they are applied to user-defined data types.
Operators may be considered as functions internal to the compiler.

Q.5 Explain indexer and property in detail.


Ans: Indexer:
An indexer allows an object to be indexed such as an array. When you define
an indexer for a class, this class behaves similar to a virtual array. You can then
access the instance of this class using the array access operator ([ ]).
Syntax:
element-type this[int index]
{
// The get accessor.
Get
{
// return the value specified by index
}
// The set accessor.
Set
{
// set the value specified by index
}
}
Use of Indexers:
o Declaration of behavior of an indexer is to some extent similar to a
property. similar to the properties, you use get and set accessors for
defining an indexer. However, properties return or set a specific data
member, whereas indexers returns or sets a particular value from the
object instance. In other words, it breaks the instance data into smaller
parts and indexes each part, gets or sets each part.
o Defining a property involves providing a property name. Indexers are not
defined with names, but with the this keyword, which refers to the
object instance. The following example demonstrates the concept −
using System;
namespace IndexerApplication
{
class IndexedNames
{
private string[] namelist = new string[size];
static public int size = 10;
public IndexedNames()
{
for (int i = 0; i < size; i++)
namelist[i] = "N. A.";
}
public string this[int index]
{
get
{
string tmp;
if (index >= 0 && index <= size - 1)
{
tmp = namelist[index];
}
else
{
tmp = "";
}

return (tmp);
}
set
{
if (index >= 0 && index <= size - 1)
{
namelist[index] = value;
}
}
}
static void Main(string[] args)
{
IndexedNames names = new IndexedNames();
names[0] = "Priyanka";
names[1] = "Deepali";
names[2] = "sakshi";
names[3] = "Vishakha";
names[4] = "Vaibhavi";
names[5] = "Amrit";
for (int i = 0; i < IndexedNames.size; i++)
{
Console.WriteLine(names[i]);
}
Console.ReadKey();
}
}
}
Output:

Overloaded Indexers:
o Indexers can be overloaded. Indexers can also be declared with multiple
parameters and each parameter may be a different type. It is not
necessary that the indexes have to be integers. C# allows indexes to be
of other types, for example, a string.
o The following example demonstrates overloaded indexers −
using System;
namespace IndexerApplication
{
class IndexedNames
{
private string[] namelist = new string[size];
static public int size = 10;
public IndexedNames()
{
for (int i = 0; i < size; i++)
{
namelist[i] = "N. A.";
}
}
public string this[int index]
{
get
{
string tmp;

if( index >= 0 && index <= size-1 )


{
tmp = namelist[index];
} else
{
tmp = "";
}

return ( tmp );
}
set
{
if( index >= 0 && index <= size-1 )
{
namelist[index] = value;
}
}
}
public int this[string name]
{
get
{
int index = 0;

while(index < size)


{
if (namelist[index] == name)
{
return index;
}
index++;
}
return index;
}
}
static void Main(string[] args)
{
IndexedNames names = new IndexedNames();
names[0] = "Priyanka";
names[1] = "Deepali";
names[2] = "Sakshi";
names[3] = "Vaibhavi";
names[4] = "Vishakha";
names[5] = "Amrit";
for (int i = 0; i < IndexedNames.size; i++)
{
Console.WriteLine(names[i]);
}
Console.WriteLine(names["Nuha"]);
Console.ReadKey();
}
}
}
Output:

Properties:
Properties are named members of classes, structures, and interfaces. Member
variables or methods in a class or structures are called Fields. Properties are an
extension of fields and are accessed using the same syntax. They
use accessors through which the values of the private fields can be read,
written or manipulated.
Properties do not name the storage locations. Instead, they have accessorsthat
read, write, or compute their values.
For example, let us have a class named Student, with private fields for age,
name, and code. We cannot directly access these fields from outside the class
scope, but we can have properties for accessing these private fields.
Accessors:
The accessor of a property contains the executable statements that helps in
getting (reading or computing) or setting (writing) the property. The accessor
declarations can contain a get accessor, a set accessor, or both. For example –
// Declare a Code property of type string:
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
Example:
The following example demonstrates use of properties −
using System;
namespace tutorialspoint
{
class Student
{
private string code = "N.A";
private string name = "not known";
private int age = 0;
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public override string ToString()
{
return "Code = " + Code + ", Name = " + Name + ", Age = " + Age;
}
}
class ExampleDemo
{
public static void Main()
{
Student s = new Student();
s.Code = "001";
s.Name = "Pratik";
s.Age = 9;
Console.WriteLine("Student Info: {0}", s);
s.Age += 1;
Console.WriteLine("Student Info: {0}", s);
Console.ReadKey();
}
}
}
Output:

Abstract Properties:
An abstract class may have an abstract property, which should be
implemented in the derived class. The following program illustrates this −
using System;
namespace tutorialspoint
{
public abstract class Person
{
public abstract string Name
{
get;
set;
}
public abstract int Age
{
get;
set;
}
}
class Student : Person
{
private string code = "N.A";
private string name = "N.A";
private int age = 0;
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
public override string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public override int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public override string ToString()
{
return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
}
}
class ExampleDemo
{
public static void Main()
{
Student s = new Student();
s.Code = "001";
s.Name = "Zara";
s.Age = 9;
Console.WriteLine("Student Info:- {0}", s);
s.Age += 1;
Console.WriteLine("Student Info:- {0}", s);
Console.ReadKey();
}
}
}
Output:

Note: A property is like a "virtual field" that contains get and set accessors and
provides an interface to the members of a class. They can be used to get and
set values to and from the class members. Properties can be static or instance
members of a class. The get accessor does not accept any parameter; rather it
returns the value of the member that it is associated with. The set accessor
contains an implicit parameter called 'value'.

Das könnte Ihnen auch gefallen