Unit - 1 Introduction
Unit - 1 Introduction
The following reasons make C# a widely used professional language (Features of C#):
1. It is a modern, general-purpose programming language
2. It is object oriented.
3. It is component oriented.
4. It is easy to learn.
5. It is a structured language.
6. It produces efficient programs.
7. It can be compiled on a variety of computer platforms.
8. It is a part of .Net Framework.
The core libraries are sometimes collectively called the Base Class Library (BCL). The entire
framework is called the Framework Class Library (FCL).
Xamarin
For writing mobile apps that target iOS, Android, and Windows Mobile. The Xamarin company
was purchased by Microsoft in 2016.
Table 1-1 compares the current platform support for each of the major frameworks
Step 2: Now, we have to alter the “Path” variable under System variables so that it also
contains the path to the .NET Framework environment. Select the “Path” variable and click
on the Edit button as shown below:
Step 1: Open the text editor like Notepad or Notepad++, and write the code that you want to
execute. Now save the file with .cs extension.
If your program has no error then it will create a filename.exe file in the same directory where you
have saved your program. Suppose you saved the above program as Hello.cs. So you will write csc
Hello.cs on cmd. This will create a Hello.exe file.
Constructors
A constructor in C# is a block of code similar to a method that’s called when an instance of an
object is created. Here are the key differences between a constructor and a method:
• A constructor doesn’t have a return type.
• The name of the constructor must be the same as the name of the class.
• Unlike methods, constructors are not considered members of a class.
• A constructor is called automatically when a new instance of an object is created.
All classes have constructors, whether you define one or not, because C# automatically
provides a default constructor that initializes all member variables to zero. However, once
you define your own constructor, the default constructor is no longer used.
Default Constructor
The default constructor is a constructor that is automatically generated in the absence of
explicit constructors (i.e. no user defined constructor). The automatically provided
constructor is called sometimes a nullary constructor.
Following is the syntax of a default constructor –
Instance Constructors
Constructors run initialization code on a class or struct. A constructor is defined like a method,
except that the method name and return type are reduced to the name of the enclosing type:
Overloaded Constructors
A class or struct may overload constructors. To avoid code duplication, one constructor may
call another, using this keyword:
When one constructor calls another, the called constructor executes first.
Destructor
Destructors in C# are methods inside the class used to destroy instances of that class when
they are no longer needed. The Destructor is called implicitly by the .NET Framework’s
Garbage collector and therefore programmer has no control as when to invoke the
destructor. An instance variable or an object is eligible for destruction when it is no longer
reachable.
Important Points:
• A Destructor is unique to its class i.e. there cannot be more than one destructor in a
class.
• A Destructor has no return type and has exactly the same name as the class name
(Including the same case).
• It is distinguished apart from a constructor because of the Tilde symbol (~) prefixed to
its name.
• A Destructor does not accept any parameters and modifiers.
• It cannot be defined in Structures. It is only used with classes.
• It cannot be overloaded or inherited.
• It is called when the program exits.
• Internally, Destructor called the Finalize method on the base class of object.
Example:
class ConsDes
{
//constructor
public ConsDes(string message)
{
Console.WriteLine(message);
}
//destructor
~ConsDes()
{
Console.WriteLine("This is a destructor");
Console.ReadKey();
}
}
class Construct
{
static void Main(string[] args)
{
string msg = "This is a constructor";
ConsDes obj = new ConsDes(msg);
obj.test();
}
}
Static Constructor
In c#, Static Constructor is used to perform a particular action only once throughout the
application. If we declare a constructor as static, then it will be invoked only once irrespective
of number of class instances and it will be called automatically before the first instance is
created.
Generally, in c# the static constructor will not accept any access modifiers and parameters. In
simple words we can say it’s a parameter less.
Example
class User
{
// Static Constructor
static User()
{
Console.WriteLine("I am Static Constructor");
}
// Default Constructor
public User()
{
Console.WriteLine("I am Default Constructor");
}
}
class Program
{
static void Main(string[] args)
{
Copy Constructor
A constructor that creates an object by copying variables from another object or that copies
the data of one object into another object is termed as the Copy Constructor. It is a
parameterized constructor that contains a parameter of the same class type. The main use of
copy constructor is to initialize a new instance to the values of an existing instance. Normally,
C# does not provide a copy constructor for objects, but if you want to create a copy
constructor in your program you can create according to your requirement.
Example:
using System;
class A{
private int a,b,sum;
//copy consturctor
public A(A obj){
sum=obj.a+obj.b;
//method
public void Display(){
Console.WriteLine("Sum={0}",sum);
}
Output:
Sum=30
Properties in C#
Properties look like fields from the outside, but internally they contain logic, like methods
do. 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(get and set) through which the
values of the private fields can be read, written or manipulated.
Usually, inside a class, we declare a data field as private and will provide a set of public SET
and GET methods to access the data fields. This is a good programming practice since the data
fields are not directly accessible outside the class. We must use the set/get methods to access
the data fields.
An example, which uses a set of set/get methods, is shown below.
Output:
10
Example of Properties:
class Chk
{
public int a { get;set;}
public int b { get; set; }
public int sum
{
get { return a + b; }
}
class Test
{
static void Main()
{
Chk obj = new Chk();
obj.a = 10;
obj.b = 5;
Console.WriteLine("Sum of "+obj.a+" and "+obj.b+" =
"+obj.sum);
Console.ReadKey();
}
}
Output:
Sum of 10 and 5 = 15
Output:
15
Arrays
An array represents a fixed number of variables (called elements) of a particular type. The
elements in an array are always stored in a contiguous block of memory, providing highly
efficient access.
An array is denoted with square brackets after the element type. For example:
Square brackets also index the array, accessing a particular element by position:
This prints “e” because array indexes start at 0. We can use a for loop statement to iterate
through each element in the array. The for loop in this example cycles the integer i from 0 to
4:
An array initialization expression lets you declare and populate an array in a single step:
Rectangular arrays
Rectangular arrays are declared using commas to separate each dimension. The following
declares a rectangular two-dimensional array, where the dimensions are 3 by 3:
A rectangular array can be initialized as follows (to create an array identical to the previous
example):
Example Program,
The inner dimensions aren’t specified in the declaration because, unlike a rectangular array,
each inner array can be an arbitrary length. Each inner array is implicitly initialized to null
rather than an empty array.
A jagged array can be initialized as follows (to create an array identical to the previous
example with an additional element at the end):
Example Program,
Strings
In C#, a string is a series of characters that is used to represent text. It can be a character, a
word or a long passage surrounded with the double quotes.
The maximum size of a String object in memory is 2GB or about 1 billion characters. However,
practically it will be less depending upon CPU and memory of the computer.
In C#, a string is a collection or an array of characters. So, string can be created using a char
array or accessed like a char array.
In c#, the string is immutable, which means the string object cannot be modified once it is
created. If any changes are made to the string object, like adding or modifying an existing
value, it will simply discard the old instance in memory and create a new instance to hold the
new value.
C# String Properties
Property Description
Chars It helps us to get the characters from the current string object based on the
specified position.
Length It returns the number of characters in the current String object.
The following table lists important string methods available in the c# programming language.
Method Description
Compare(String, It compares two specified String objects and returns an integer that
String) indicates their relative position in the sort order.
Concat(String, It concatenates two specified instances of String.
String)
Contains(String) It returns a value indicating whether a specified substring occurs within
this string. Returns true or false.
Copy(String) It creates a new instance of String with the same value as a specified
String.
Trim() It removes all leading and trailing white-space characters from the
current String object.
ToLower() It converts a given string to a lowercase.
ToUpper() It converts a given string to uppercase.
Split(Char[]) It splits a string into substrings that are based on the characters in
an array.
Substring(int) It retrieves a substring from this instance. The substring starts at a
specified character position and continues to the end of the string.
Example of Compare:
Condition Value
strA == strB 0
using System;
public class Program
{
public static void Main()
{
string str1="Ram";
string str2="Shyam";
Console.WriteLine(string.Compare(str1,str2));
}
}
Output:
-1
Output:
Raju Poudel
Example of Substring
using System;
public class Program
{
public static void Main()
{
string str1="I love my country";
//Substring(startindex,length);
string str2=str1.Substring(2,7);
Console.WriteLine(str2);
}
}
Output:
love my
Indexers in C#
In c#, Indexer is a special type of property, and that allows instances of a class or structure
to be indexed same like an array. In c#, the indexer is same as the property, but the only
difference is, the indexer will define this keyword along with the square bracket and
parameters.
The Indexers in C# are the members of a class and if we define indexers in a class then the
class behaves like a virtual array. So, it’s a member of a class that gives access to the values
of a class just like an array.
C# indexers are usually known as smart arrays. A C# indexer is a class property that allows
you to access a member variable of a class or struct using the features of an array. In C#,
indexers are created using this keyword. Indexers in C# are applicable on both classes and
structs.
Example of Indexer
Let’s examine following example to understand indexer.
First let’s run below code that doesn’t use indexer.
using System;
class Student{
public int sid{get;set;}
public string name{get;set;}
public string address{get;set;}
}
This is because we cannot apply indexing directly to a class. We can do indexing on an array
but we cannot do the same thing with a user-defined class like Student.
An array is a predefined class and all the logic’s are implemented in that class for indexing so
that we can access them using indexes. But Student is a user-defined class and we have not
implemented any logic to access the class like an array.
If you want to access the class like an array then first you need to define an indexer in the
class. Once you define an indexer in the class then you can start accessing the values of the
class by using the index position.
using System;
class Student{
public int sid{get;set;}
public string name{get;set;}
public string address{get;set;}
Output:
101 Ram Birtamod
The : indicates that you are making a new class that derives from an existing class. The
meaning of "extends" is to increase the functionality.
In the terminology of C#, a class which is inherited is called a parent or superclass, and the
new class is called child or subclass.
Types of inheritance in C#
On the basis of class, there can be three types of inheritance in java: single, multilevel and
hierarchical.
In C# programming, multiple and hybrid inheritance is supported through interface only.
using System;
class A
{
public int a=10, b=5;
}
class B : A
{
int a=30, b=5;
public void test()
{
Console.WriteLine("Value of a is: "+a);
Console.WriteLine("Value of a is: " + base.a);
}
}
//driver class
class Inherit
{
static void Main(string[] args)
{
B obj = new B();
2. Multilevel Inheritance
Multilevel inheritance refers to a child and parent class relationship where a class extends the
child class. For example, class C extends class B and class B extends class A.
class A
{
public int a, b, c;
class B : A
{
public void Add()
{
base.c = base.a + base.b;
Console.WriteLine("Sum="+base.c);
}
}
class C : B
{
public void Sub()
{
base.c = base.a - base.b;
Console.WriteLine("Difference=" + base.c);
}
}
class Level
{
static void Main()
{
A7 obj = new A7();
obj.ReadData(20,5);
obj.Display();
Console.ReadLine();
}
}
3. Hierarchical Inheritance
Hierarchical inheritance refers to a child and parent class relationship where more than one
classes extends the same class. For example, classes B, C & D extends the same class A.
class Polygon
{
public int dim1,dim2;
public void ReadDimension(int dim1, int dim2)
{
this.dim1 = dim1;
this.dim2 = dim2;
}
}
//driver class
class Hier
{
static void Main()
{
Traingle tri = new Traingle();
//tri.ReadDimension(10,5);
tri.AreaTri();
Console.ReadLine();
}
}
4. Multiple Inheritance
When one class extends more than one classes then this is called multiple inheritance. For
example: Class C extends class A and B then this type of inheritance is known as multiple
inheritance.
C# doesn’t allow multiple inheritance. We can use interfaces instead of classes to achieve the
same purpose.
interface IA
{
// doesn't contain fields
int CalculateArea();
int CalculatePerimeter();
}
class CA
{
public int l, b;
public void ReadData(int l,int b)
{
this.l = l;
this.b = b;
}
}
class BB : CA, IA
{
public int CalculateArea()
{
ReadData(10,5);
int area=l*b;
return area;
}
//driver class
class Inter
{
static void Main(string[] args)
{
Console.WriteLine("Area of Rectangle=" +
obj.CalculateArea());
Console.WriteLine("Perimeter of Rectangle=" +
obj.CalculatePerimeter());
Console.ReadKey();
}
}
using System;
class A{
public int a=10;
}
class B:A{
public int a=20;
public void Display(){
Console.WriteLine("A in base class={0}",base.a);
Console.WriteLine("A in derived class={0}",a);
}
}
Output:
A in base class=10
A in derived class=20
Method Overloading
Method Overloading is a feature that allows a class to have more than one method having
the same name, if their argument lists are different. It is similar to constructor overloading in
Java, that allows a class to have more than one constructor having different argument lists.
In order to overload a method, the argument lists of the methods must differ in either of
these:
a) Number of parameters.
add(int, int)
add(int, int, int)
b) Data type of parameters.
add(int, int)
add(int, float)
c) Sequence of Data type of parameters.
add(int, float)
add(float, int)
using System;
class A{
public void Add(int a,int b){
int sum=a+b;
Console.WriteLine("Sum={0}",sum);
}
Output:
Sum=30
Sum=60
Sum=21.7
Method Overriding
Method overriding in C# allows programmers to create base classes that allows its inherited
classes to override same name methods when implementing in their class for different
purpose. This method is also used to enforce some must implement features in derived
classes.
Important points:
• Method overriding is only possible in derived classes, not within the same class where
the method is declared.
• Base class must use the virtual or abstract keywords to declare a method. Then only
can a method be overridden
class Test
{
static void Main()
{
Amount obj = new Amount();
int balance = obj.balance();
Console.WriteLine("Balance is: "+balance);
Console.ReadKey();
}
}
Output:
Balance is 500
Virtual Method
A virtual method is a method that can be redefined in derived classes. A virtual method has
an implementation in a base class as well as derived the class. It is used when a method's
basic functionality is the same but sometimes more functionality is needed in the derived
class. A virtual method is created in the base class that can be overriden in the derived class.
We create a virtual method in the base class using the virtual keyword and that method is
overriden in the derived class using the override keyword.
class Vir
{
public virtual void message()
{
Console.WriteLine("This is test");
}
}
class Program
{
static void Main(string[] args)
{
Vir obj = new Vir();
obj.message();
Console.ReadKey();
}
}
class Vir
{
public virtual void message()
{
Console.WriteLine("This is test");
}
}
class Program
{
static void Main(string[] args)
{
Vir1 obj = new Vir1();
obj.message();
Console.ReadKey();
}
}
Output:
This is test1
Abstract Class
A class declared as abstract can never be instantiated. We can’t create object of abstract
class.
If a class is defined as abstract then we can't create an instance of that class. By the creation
of the derived class object where an abstract class is inherited from, we can call the method
of the abstract class.
Abstract class can be created using abstract keyword.
using System;
abstract class A{
public void Message1(){
Console.WriteLine("Inside class A");
}
}
class B : A{
public void Message2(){
Console.WriteLine("Inside class B");
}
}
Output:
Inside class A
Inside class B
Abstract Members
An Abstract method is a method without a body. The implementation of an abstract method
is done by a derived class. When the derived class inherits the abstract method from the
abstract class, it must override the abstract method. This requirement is enforced at compile
time and is also called dynamic polymorphism.
using System;
abstract class A{
//abstract method
public abstract void Add(int a,int b);
public abstract void Display();
}
class B:A{
int sum;
public override void Add(int a,int b){
sum=a+b;
}
public override void Display(){
Console.WriteLine("Sum={0}",sum);
}
}
Output:
Sum=30
Like a class, an interface can have methods and properties, but the methods declared in
interface are by default abstract (only method signature, no body).
v Interfaces specify what a class must do and not how. It is the blueprint of the class.
v An Interface is about capabilities like a Player may be an interface and any class
implementing Player must be able to (or must implement) move (). So it specifies a
set of methods that the class has to implement.
v If a class implements an interface and does not provide method bodies for all functions
specified in the interface, then class must be declared abstract.
interface IA
{
// doesn't contain fields
void GetData(int l,int b);
int CalculateArea();
int CalculatePerimeter();
}
class BB : IA
{
int l, b;
public void GetData(int l, int b)
{
this.l = l;
this.b = b;
}
public int CalculateArea()
{
int area=l*b;
return area;
}
Console.ReadKey();
}
}
Output:
Area of Rectangle=50
Perimeter of Rectangle=30
Sealed Class
Sealed classes are used to restrict the inheritance feature of object oriented programming.
Once a class is defined as a sealed class, this class cannot be inherited. In C#, the sealed
modifier is used to declare a class as sealed. If a class is derived from a sealed class, compiler
throws an error.
If you have ever noticed, structs are sealed. You cannot derive a class from a struct.
// Sealed class
sealed class SealedClass{
}
using System;
sealed class A{
int sum;
public void Add(int a,int b){
sum=a+b;
Console.WriteLine("Sum={0}",sum);
}
}
Output:
Sum=30
In C#, we can use partial keyword to split the definition of a particular class, structure,
interface, or method over two or more source files.
using System;
partial class A{
int sum;
public void Add(int a, int b){
sum=a+b;
}
}
partial class A{
public void Display(){
Console.WriteLine("Sum={0}",sum);
}
}
Output:
Sum=30
Structs
A struct is similar to a class, with the following key differences:
• A struct is a value type, whereas a class is a reference type.
• A struct does not support inheritance
.
A struct can have all the members a class can, except the following:
• A parameter less constructor
• Field initializers
• A finalizer
• Virtual or protected members
Enums in C#
An enum is a special "class" that represents a group of constants (unchangeable/read-only
variables).
To create an enum, use the enum keyword (instead of class or interface), and separate the
enum items with a comma:
enum Level
{
Low,
Medium,
High
}
class Program
{
static void Main(string[] args)
{
Level myVar = Level.Medium;
Console.WriteLine(myVar);
}
}
Output:
Medium
class Program
{
enum Level
{
Low,
Medium,
High
}
static void Main(string[] args)
{
Level myVar = Level.Medium;
Console.WriteLine(myVar);
}
}
Output:
Medium
Delegates
A delegate is an object that knows how to call a method.
A delegate type defines the kind of method that delegate instances can call. Specifically, it
defines the method’s return type and its parameter types.
Delegates are especially used for implementing events and the call-back methods. All
delegates are implicitly derived from the System.Delegate class. It provides a way which tells
which method is to be called when an event is triggered.
For example, if you click an Button on a form (Windows Form application), the program
would call a specific method.
In simple words, it is a type that represents references to methods with a particular parameter
list and return type and then calls the method in a program for execution when it is needed.
Declaring Delegates
Delegate type can be declared using the delegate keyword. Once a delegate is declared,
delegate instance will refer and call those methods whose return type and parameter-list
matches with the delegate declaration.
Syntax:
[modifier] delegate [return_type] [delegate_name] ([parameter_list]);
modifier: It is the required modifier which defines the access of delegate and it is optional to
use.
delegate: It is the keyword which is used to define the delegate.
return_type: It is the type of value returned by the methods which the delegate will be going
to call. It can be void. A method must have the same return type as the delegate.
delegate_name: It is the user-defined name or identifier for the delegate.
parameter_list: This contains the parameters which are required by the method when called
through the delegate.
Note: A delegate will call only a method which agrees with its signature and return type. A
method can be a static method associated with a class or can be an instance method
associated with an object, it doesn’t matter.
Transformer is compatible with any method with an int return type and a single int
parameter, such as this:
static int Square (int x) { return x * x; }
or
static int Square (int x) => x * x;
Output:
Result is : 25
Example 2
using System;
class TestDelegate {
static int num = 10;
Output:
Value of Num: 35
Value of Num: 175
class Test
{
public delegate void addnum(int a, int b);
public delegate void subnum(int a, int b);
// method "sum"
public void sum(int a, int b)
{
Console.WriteLine("(100 + 40) = {0}", a + b);
}
// method "subtract"
public void subtract(int a, int b)
{
Console.WriteLine("(100 - 60) = {0}", a - b);
}
// Main Method
public static void Main(String[] args)
{
addnum del_obj1 = new addnum(obj.sum);
subnum del_obj2 = new subnum(obj.subtract);
Output:
(100 + 40) = 140
(100 - 60) = 40
Multicast Delegates
All delegate instances have multicast capability. This means that a delegate instance can
reference not just a single target method, but also a list of target methods.
The + and += operators combine delegate instances. For example:
SomeDelegate d = SomeMethod1;
d += SomeMethod2;
using System;
nc = nc1;
nc += nc2;
//calling multicast
nc(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
Output
Value of Num: 75
The async and await keywords in C# are used in async programming. Using them, you can
work with .NET Framework resources, .NET Core, etc. Asynchronous methods defined using
the async keyword are called async methods.
Nowadays, Asynchronous programming is very popular with the help of the async and await
keywords in C#. When we are dealing with UI, and on button click, we use a long-running
method like reading a large file or something else which will take a long time, in that case, the
entire application must wait to complete the whole task. In other words, if any process is
blocked in a synchronous application, the whole application gets blocked, and our application
stops responding until the whole task completes.
We will get all the benefits of traditional Asynchronous programming with much less effort
with the help of async and await keywords.
Suppose we are using two methods as Method1 and Method2 respectively, and both the
methods are not dependent on each other, and Method1 takes a long time to complete its
task. In Synchronous programming, it will execute the first Method1 and it will wait for the
completion of this method, and then it will execute Method2. Thus, it will be a time-intensive
process even though both methods are not depending on each other.
using System;
using System.Threading.Tasks;
class A{
int sum;
public async Task Add(int a,int b){
sum=a+b;
}
Output:
Sum=50
Collections in C#
C# collection types are designed to store, manage and manipulate similar data more
efficiently. Data manipulation includes adding, removing, finding, and inserting data in the
collection.
Collection types implement the following common functionality:
• Adding and inserting items to a collection
• Removing items from a collection
• Finding, sorting, searching items
• Replacing items
• Copy and clone collections and items
• Capacity and Count properties to find the capacity of the collection and number of
items in the collection
.NET supports two types of collections, generic collections and non-generic collections. Prior
to .NET 2.0, it was just collections and when generics were added to .NET, generics collections
were added as well.
C# Non-Generic Collections
In c#, non-generic collection classes are useful to store elements of different data types, and
these are provided by System.Collections namespace.
Class Description
ArrayList It is useful to represent an array of objects whose size is dynamically increased as
required.
Queue It is useful to represent a FIFO (First In, First Out) collection of objects.
Stack It is useful to represent a LIFO (Last in, First Out) collection of objects.
Hashtable It is useful to represent a collection of key/value pairs organized based on the key's hash
code.
C# Generic Collections
In c#, generic collections will enforce a type safety so we can store only the elements with the
same data type, and these are provided by System.Collections.Generic namespace.
Class Description
List It is useful to represent a list of objects that can be accessed by an index.
Queue It is useful to represent a FIFO (First In, First Out) collection of objects.
Stack It is useful to represent a LIFO (Last in, First Out) collection of objects.
SortedList<K,V> It is useful to represent a collection of key/value pairs sorted by a key.
Dictionary<K,V> It is useful to represent a collection of key/value pairs organized based on the key.
using System.Collections.Generic;
public class Program
{
static void Main(string[] args)
{
//define Dictionary collection
Dictionary<int, string> dObj = new Dictionary<int,
string>(5);
//add elements to Dictionary
dObj.Add(1, 1, "Tom");
dObj.Add(2, "John");
dObj.Add(3, "Maria");
dObj.Add(4, "Max");
dObj.Add(5, "Ram");
//print data
for (int i = 1; i <= dObj.Count; i++)
{
Console.WriteLine(dObj[i]);
}
Console.ReadKey();
}
}
Queues
Queues are a special type of container that ensures the items are being accessed in a FIFO
(first in, first out) manner. Queue collections are most appropriate for implementing
messaging components. We can define a Queue collection object using the following syntax:
Queue qObj = new Queue();
The Queue collection property, methods and other specification definitions are found under
the Sysyem.Collection namespace. The following table defines the key members;
Stacks
A Stack collection is an abstraction of LIFO (last in, first out). We can define a Stack collection
object using the following syntax:
Stack qObj = new Stack();
The following table illustrates the key members of a stack;
class Program {
static void Main(string[] args) {
Stack < string > stack1 = newStack < string > ();
stack1.Push("************");
stack1.Push("MCA");
stack1.Push("MBA");
stack1.Push("BCA");
stack1.Push("BBA");
foreach(string s in stack1) {
Console.WriteLine(s);
}
}
}
List
List<T> class in C# represents a strongly typed list of objects. List<T> provides functionality to
create a list of objects, find list items, sort list, search list, and manipulate list items. In List<T>,
T is the type of objects.
Adding Elements
// Dynamic ArrayList with no size limit
List<int> numberList = new List<int>();
numberList.Add(32);
numberList.Add(21);
numberList.Add(45);
numberList.Add(11);
numberList.Add(89);
// List of string
List<string> authors = new List<string>(5);
authors.Add("Mahesh Chand");
authors.Add("Chris Love");
authors.Add("Allen O'neill");
authors.Add("Naveen Sharma");
authors.Add("Monica Rathbun");
authors.Add("David McCarter");
// Collection of string
string[] animals = { "Cow", "Camel", "Elephant" };
// Create a List and add a collection
List<string> animalsList = new List<string>();
animalsList.AddRange(animals);
foreach (string a in animalsList)
Console.WriteLine(a);
Sorting
authors.Sort();
Other Methods
authors.Insert(1,"Shaijal"); //insert item at index 1
authors.Count; //returns total items
Array List
C# ArrayList is a non-generic collection. The ArrayList class represents an array list and it can
contain elements of any data types. The ArrayList class is defined in the System.Collections
namespace. An ArrayList is dynamic array and grows automatically when new items are added
to the collection.
Insertion
personList.Add("Sandeep");
Removal
// Remove an item
personList.Remove("New Author1");
Sorting
personList.Sort();
Other Methods
personList.Insert(1,"Shaijal"); //insert item at index 1
personList.Count; //returns total items
Generic Class
The Generic class can be defined by putting the <T> sign after the class name. It isn't
mandatory to put the "T" word in the Generic type definition. You can use any word in the
TestClass<> class declaration.
public class TestClass<T> { }
using System.Collections.Generic;
class Test<T>
{
T[] t=new T[5];
int count = 0;
public void addItem(T item)
{
if (count < 5)
{
t[count] = item;
count++;
}
else
{
Console.WriteLine("Overflow exists");
}
}
class GenericEx
{
static void Main()
{
Test<int> obj = new Test<int>();
obj.addItem(10);
obj.addItem(20);
obj.addItem(30);
obj.addItem(40);
obj.addItem(50);
//obj.addItem(60); //overflow exists
obj.displayItem();
Console.ReadKey();
}
}
Output
Item at index 0 is 10
Item at index 1 is 20
Item at index 2 is 30
Item at index 3 is 40
Item at index 4 is 50
For example, SQL is a Structured Query Language used to save and retrieve data from a
database. In the same way, LINQ is a structured query syntax built in C# to retrieve data from
different types of data sources such as collections, ADO.Net DataSet, XML Docs, web service
and MS SQL Server and other databases.
LINQ queries return results as objects. It enables you to uses object-oriented approach on the
result set and not to worry about transforming different formats of results into objects.
The following example demonstrates a simple LINQ query that gets all strings from an
array which contains 'a'.
LINQ Operators
Classification LINQ Operators
Filtering Where, OfType
Sorting OrderBy, OrderByDescending, ThenBy, ThenByDescending, Reverse
Grouping GroupBy, ToLookup
Join GroupJoin, Join
Projection Select, SelectMany
Aggregation Aggregate, Average, Count, LongCount, Max, Min, Sum
Quantifiers All, Any, Contains
Elements ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last,
LastOrDefault, Single, SingleOrDefault
Set Distinct, Except, Intersect, Union
Partitioning Skip, SkipWhile, Take, TakeWhile
Concatenation Concat
Equality Equals,SequenceEqual
Generation DefaultEmpty, Empty, Range, Repeat
Conversion AsEnumerable, AsQueryable, Cast, ToArray, ToDictionary, ToList
class LambdaTest
{
static int test1() => 5;
static int test2(int x) => x + 10;
Output:
Result is: 15
Unlike an expression lambda, a statement lambda can contain multiple statements separated
by semicolons. It is used with delegates.
Exception Handling
A try statement specifies a code block subject to error-handling or clean-up code. The try
block must be followed by a catch block, a finally block, or both. The catch block executes
when an error occurs in the try block. The finally block executes after execution leaves the try
block (or if present, the catch block), to per- form clean-up code, whether or not an error
occurred.
A catch block has access to an Exception object that contains information about the error.
You use a catch block to either compensate for the error or re throw the exception. You re
throw an exception if you merely want to log the problem, or if you want to re throw a new,
higher-level exception type.
A finally block adds determinism to your program: the CLR endeavours to always execute it.
It’s useful for clean-up tasks such as closing network connections.
The following table provides some of the predefined exception classes derived from the
Sytem.SystemException class –
Attributes in C#
Attributes are used in C# to convey declarative information or metadata about various code
elements such as methods, assemblies, properties, types, etc.
Attributes are added to the code by using a declarative tag that is placed using square brackets ([ ])
on top of the required code element.
There are two types of Attributes implementations provided by the .NET Framework are:
1. Predefined Attributes
2. Custom Attributes
Properties of Attributes:
• Attributes can have arguments just like methods, properties, etc. can have arguments.
• Attributes can have zero or more parameters.
• Different code elements such as methods, assemblies, properties, types, etc. can have one or
multiple attributes.
• Reflection can be used to obtain the metadata of the program by accessing the attributes at
run-time.
• Attributes are generally derived from the System.Attribute Class.
Predefined Attributes
Predefined attributes are those attributes that are a part of the .NET Framework Class Library
and are supported by the C# compiler for a specific purpose.
Attributes Description
[WebMethod] This is used to create XML web services. Also, the marked method is
invoked by the HTTP request.
[Obsolete] Any member or type can be marked as deprecated using [Obsolete].
The compiler issues a warning if Obsolete is used.
[Serializable] A class persists with its current state by marking this attribute.
[NonSerializable] A class or field should not be persisted using this attribute.
[DllImport] This allows a call by the code to the unmanaged library.
using System;
class A{
[Obsolete("Do not write Test as method!",false)]
public void Test(){
Console.WriteLine("Inside class A!");
}
}
Output:
main.cs(10,13): warning CS0618: ` A.Test()' is obsolete: `Do not write Test
as method!'
Inside class A
Attribute Class
In an attribute class, you will define the members that support the attribute.
Steps for creating Custom Attributes:
• Define a custom attribute class that is derived from System.Attribute class.
• The custom attribute class name should have the suffix Attribute.
• Use the attribute AttributeUsage to specify the usage of the custom attribute class created.
• Create the constructor and the accessible properties of the custom attribute class
using System;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
AttributeTargets.Constructor)]
//[AttributeUsage(AttributeTargets.All)]
class InfoAttribute : Attribute{
string comment{get;set;}
public InfoAttribute(string comment){
this.comment=comment;
}
}
Output:
Name is:Ram
Attribute Targets
These are the most common attribute targets, or pieces of code infrastructure that can be
marked with an attribute.
Target Description
Type Attributes belong to a delegate, class, struct, interface, or enum
Method Attributes relate to a method or property accessors (get / set)
Property Attributes relate to properties
Assembly Attributes relate to the entire assembly
Field Attributes relate to a field on a class or struct
Return Attributes relate to the return value of a method or property accessors
Event Attributes relate to an event
Parameter Attributes relate to parameters of methods or property accessors
Example is already shown above.
[Serializable, Obsolete]
[CLSCompliant(false)]
public class Bar {...}
When specifying an attribute, you must include positional parameters that correspond to one
of the attribute's constructors.
Named parameters are optional.
In the above example, the first argument is a positional parameter ; the second is a named
parameter.
using System;
[AttributeUsage(AttributeTargets.All)]
class InfoAttribute : Attribute{
public string comment{get;set;}
}
Output:
Inside class A!
StreamWriter Class
The StreamWriter class implements TextWriter for writing character to stream in a particular format.
The class contains the following method which are mostly used.
Method Description
Close() Closes the current StreamWriter object and stream associate with it.
Flush() Clears all the data from the buffer and write it in the stream associate with it.
Write() Write data to the stream. It has different overloads for different data types
to write in stream.
WriteLine() It is same as Write() but it adds the newline character at the end of the data.
using System;
using System.IO;
class WriteToFile {
public void Data()
{
// This will create a file named sample.txt
// at the specified location
StreamWriter sw = new StreamWriter("D://test.txt");
Method Description
Close() Closes the current StreamReader object and stream associate with it.
Peek() Returns the next available character but does not consume it.
Read() Reads the next character in input stream and increment characters position by one in
the stream
ReadLine() Reads a line from the input stream and return the data in form of string
Seek() It is use to read/write at the specific location from a file
using System;
using System.IO;
class ReadFile {
// Main Method
static void Main(string[] args)
{
ReadFile wr = new ReadFile();
wr.DataReading();
}
}