Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
C# (pronounced C-sharp) is a one kind of objects oriented language developed by Microsoft Corporation.
It is designed with the combination of C and C++. It has taken a lot of concepts from Java.
C# Data Types
In a programming language Data Types describes what type of data a variable can hold. C# is a
strongly typed language. That means every variable and object must have a declared type. When
we declare a variable, we have to tell the compiler about what type of the data the variable can hold.
In C# programming language, Data types are divided into three categories:
1. Value Type
2. Reference Type
3. Pointer Type
Value Type
A Value Type variable stores a copy of the value in the memory. Some common examples are int,
char, float. When we declare an int type, the system allocates memory to store the value.
Reference Type
A Reference Type stores the address of the value in the memory. It does not contain the actual data
stored in a variable. They contain a reference to the variables. Some built-in reference types are:
object, dynamic and string. The user-defined reference types are: class, interface, delegate, Array.
Pointer Type
Pointer type variables store the memory address of another type. Pointers in C# have the same
capabilities as in C or C++.
Object
Object is a built-in reference data type. It is a base class for all predefined and user-defined data
types. A Class is a logical structure that represents a real world entity. This means that the
predefined and user-defined data types are created based on the object class.
String
String is a built-in reference data type. A string type signifies Unicode character string values. It
allows you to assign and manipulate string values. Once strings are created, they cannot be
modified.
Class
A Class is user-defined structure that contains variables and methods.
Delegate
A Delegate is a user-defined reference type that stores the reference of one or more methods.
Interface
An interface is a type of user-defined class that is used for multiple inheritance.
Array
An array is a user-defined data structure that contains values of the same data type, such as marks
of Student.
What is Enumerations or enums?
Enumerations or enums is a set of named integer constants. Enumerated type is declared using the
enum keyword. In C# enums are value types and enum constants must be integral numeric values.
Its index starts from 0;
Example
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
Output
int WeekdayStart = (int)Days.Mon;
int WeekdayEnd = (int)Days.Fri;
Console.WriteLine(“Monday: {0}”, WeekdayStart);
Console.WriteLine(“Friday: {0}”, WeekdayEnd);
Monday: 1
Friday: 5
What is HashTable?
A hashtable or a hash map is a data structure that is used to implement an associative array. It
associates keys with values. The primary operation in hash table is look like: given a key (e.g. a
student’s name), find the corresponding value (e.g. that student’s ID number). It uses a hash
function to compute an index into an array of buckets or slots, from which the correct value can be
found. It is an older .NET Framework. It is slower than the generic Dictionary type.
Example:
Hashtable hashtable = new Hashtable();
hashtable.Add(100, "Dhaka");
hashtable.Add(200, "London");
Console.WriteLine(key);
Console.WriteLine(value);
Output
//First loop
100
200
//Second loop
Dhaka
London
When to use a HashTable?
When we need to search items quickly by key.
We can use IList or IEnumerable etc for a matching key but that will take O(n) time rather than O(1)
for Hashtable or Dictionary.
Differences between Hashtable and Dictionary. Hashtable vs. Dictionary
Hashtable:
1. It returns null if we try to find a key which does not exist
2. It is slower than dictionary because it requires boxing and unboxing
3. All the members in a Hashtable are thread safe
4. It is not a generic type that means we can’t use it with any data type
Dictionary:
1. It returns error if we try to find a key which does not exist
2. It is faster than a Hashtable because there is no boxing and unboxing
3. Only public static members are thread safe
4. It is a generic type that means we can use it with any data type
What is Delegates?
A delegate in C# allows you to pass method of one class to objects of other class that can call these
methods.
OR
A delegate is an object that holds the reference to a method.
In C++ it is called function pointer.
How do you inherit a class into other class in C#?
Colon(:) is used as inheritance operator in C#. To inherit a class just places a colon and then the
class name.
public class DerivedClass : BaseClass
What is the difference between method overriding and method overloading?
Overloading
1. It is a compile time polymorphism
2. At the compile time the compiler know which object is assigned for which class
3. Method name will be same, parameters will be different and its return type may or may not same
4. Example- operator overloading, function overloading
Overriding
1. It is a run time polymorphism
2. At the compile time the compiler didn’t know which object is assigned for which class. Compiler knows
it at run time
3. Method name will be same, parameters will be same and its return type will be same
4. Example- virtual function
What are Constructor & Destructor?
Constructor is a member function and its name is as like as class name. It is used to initialize the
class object. Constructor & Destructor both are necessary for every class. If we don’t create them
compiler automatically create them by himself. Constructor & Destructor has no return type. It is
called when a class object is created. Constructor & Destructor has no statement primarily. But we
can create custom statement. Constructor may have parameter or not.
Destructor is used to delete object instance from the memory. We need to destroy object instance
after its task. If we don’t does this compiler automatically do this? Its name is as like class name.
Only extra ~ sign is used before its name. It has no parameter and it doesn’t accept any operator.
For example consider the bellow class. If we call the old method it will through error message.
omyClass.Old();
//Type information.
Console.Read();
x++;
It will not provide error at compile time but will provide error at run time.
if (t1.Equals(t2))
return true;
}
else
return false;
Console.WriteLine(oComparerInt.Compare(10, 10));
Console.WriteLine(oComparerStr.Compare("jdhsjhds", "10"));
o ArrayList
o SortedList
o Hashtable
o Stack
o Queue
o BitArray
What is unsafe code?
In order to maintain security and type safety, C# does not support pointer generally. But by using
unsafe keyword we can define an unsafe context in which pointer can be used. The unsafe code or
unmanaged code is a code block that uses a pointer variable.
In the CLR, unsafe code is referred to as unverifiable code. In C#, the unsafe code is not necessarily
dangerous. The CLR does not verify its safety. The CLR will only execute the unsafe code if it is
within a fully trusted assembly. If we use unsafe code, it is our own responsibility to ensure that the
code does not introduce security risks or pointer errors.
In Visual Studio IDE at first we need to enable use of unsafe code in the project properties. The
steps are given bellow: