Introduction to C-sharp.Net Basics

First of all, we have a tendency to discuss the c# language the .Net framework, Microsoft includes a brand new language that is C-sharp. C-sharp is meant to be a straightforward, modern, all-purpose, object-oriented language.

C# may, in theory, be compiled to computer code, however, in reality additionally, it is often employed in combination with the .NET framework. Therefore, applications written in C-sharp needs the .NET framework to be put in on the PC running the applying.

Jump to Section

WHAT IS C-sharp LANGUAGE?

C-sharp

C-sharp is a vital object-oriented programing language that helps developers to form associate software application that runs on the dotNet framework. Its syntax is definitely understood, it’s kind safe in truth the code will access associate degree object that has been typewritten and allowed and thus cannot browse values from non-public fields and another object.

MEMORY MANAGEMENT IN C-sharp

WHAT IS DATA TYPE?

A data type is employed to form variables, what is more, It tells us which kind of data we are able to hold into variables.Variables measure wont to hold information.

TYPES OF DATA TYPE IN C#:

Three types of data types in c#:

  • Value Data Type
  • References Data Type
  • Pointer Data Type

Value Data Type:

Value data type is an integer, floating point based and support both signed and unsigned.

There are two types of value data type:

  1. Predefined Data Type: Example -> Integer,Float,Boolean etc.
  2. User Defined Data Type: Example -> Structure,Enum etc.

Reference Data Type:

Reference data type is those which contains a reference to the variable.

There are two types of reference data type:

  1. Predefined Data Type: Example -> String.
  2. User Defined Data Type: Example -> Classes, Interface.

Pointer Data Type:

C#.net pointer is a variable that points to an address of a value.

Character Variable:

C# Type             CTS.Net Type         Size

char                    Char                       2 Byte(Unicode System)

In C language char size is one computer memory unit, as a consequence of C language, we will handle solely 256 characters severally. Although, in C-sharp char, size have a pair of computer memory unit. It uses Unicode system thus, we will handle the majority the character within the world severally. C uses ‘ASCII‘ code system conversely, C-sharp uses multiple languages.

Special Data Type:

C# Type             CTS.Net Type         Size

bool                     Boolean                1 Byte

DateTime          DateTime              8 Byte

String:

C# Type             CTS.Net Type           Size

String                  String               N/A(Dynamic)

Declaration Of  Variable In C-sharp

Example ->
  • string name=”Raja”;
  • int number=10;
  • bool flag=true;
  • char value=’x’;

Conversion Of Data Type

byte a=10;
byte b=20;
byte c=a+b;   Error    Why

Note: In c#.net the compiler by default calculate the arithmetic expression of the correct facet of the assignment operator within the sort of associate whole number and there’s no implicit conversion of int knowledge sort into computer memory unit thus, it should be typecast into a computer memory unit.

byte c=(byte)(a+b);

Output: 30

 Array (Collection Of Similar Data Type):

  • In case of c and c++ array support static binding in other word memory allocation at compile time.
  • Array support dynamic binding in c# and java.
  • In case of c and c++ there is no default provision to check upper bound of the array.
  • In case of c#.net and Java, there has default provision to check upper bound of the array.

-> Find Address of x[i]=base address + i*size in byte

     X[0]=5; X[1]=7; X[2]=9;

  • x[0]=1000+0*2=1000
  • x[1]=1000+1*2=1002
  • x[2]=1000+2*2=1004

General Syntax To Create Array In C-sharp

Type [] name of array=new type[size];

Example ->
  • int[] intarr=new int[2];
  • string[] strarr=new string[10];

General Syntax To Resize Array In C-sharp

Array.Resize(ref name of array,new size);

Example ->
  • Array.Resize(ref arr,4);

General Syntax To Create Two Dimensional Array

Type[ ,] name of array=new type[row size,column size];

Example ->
  • int [ ,] darr=new int [3,4];

Jagged Array

An array of an associate array is understood as a jagged array. We will fix row within the jagged array whereas we will outline a variable column in every row.

General Syntax Of Jagged Array

Type [][] name of array=new type[row size][];

Example ->
  • int [][] jarr=new int[3][];

Csharp

Name Space

A namespace may be a logical instrumentality want to contain item like property structure interface enum.

Example ->
  • using system.IO
  • System.text.RegularExpression

Access specifier

Access specifier is employed to dominant the accessibility of an object and its member. What is more, with the utilization of access specifier class will hide data from another class.

Types Of Access Specifier

There are five types of access specifier in c#:

  1. Private -> Accessible with in class.
  2. Public -> Accessible Anywhere
  3. Protected -> Accessible within the class and within drive class.
  4. Internal -> Accessible within namespace or assembly.
  5. Protected Internal -> Combination of protected and internal.
  • We can not use private or protected access specifier with outer most class.
  • We can use protected or private access specifier with an inner class.

Inner Class

A class within a class is understood as an inner class.

Partial

A partial may be a keyword that uses to divide classes or interface into two or more than two parts.

Type Casting

Typecasting may be a technique by that we will convert a data type into another data type.

General Syntax For Type Casting

Convert.To Type(expression)

Example ->

cus[0].customerid= convert.ToInt32(txtcustomerid.Text);

Function

A function could be a separate block of code that uses to scale back the quality of a program. In another word, A function could be a technique by that we will connect two or more a machine, thus we tend to additionally accustomed transfer information from one machine to a different machine.

Types Of Function On The Basis Of Argument

  1. Call by Value -> A function in which we are passing value type variable or direct values is known as call by value function.
  2. Call by Reference -> Passing variable of class or reference type variable of function then the function is known as call by reference. All classes are the reference type.
  3. Call by Address -> A function in which pointer type variable or direct address is known as call by address.

C-sharp

Value Type Variable

All primitive data type and all structure type are a value type. All integer are primitive.

General Syntax Of Function In C#.Net

Access specifier   return type    name of function(comma separated argument list)
{
statement
}

Points To Remember

 When we are trying to create a function or call a function then we have to prepare three things.

  1. Name of function
  2. Input for function(Argument of function)
  3. Output of function(Return type of function)
Example ->
public int sum(no1, no2)
{
}
public string name(first_name, last_name)
{
}

Advance Concepts Of C-sharp

  1. Class
  2. Constructor
  3. Upcasting and Downcasting
  4. Boxing and Unboxing
  5. Interface
  6. Delegate
  7. Oops Concepts
  8. Property
  9. Indexer
  10. Method Hiding

Class

A class is a user-defined data type which contains the following item into it.

  • Data Field(variables)
  • Member Function
  • Property
  • Constructor
  • Destructor
  • Indexer
  • Event
  • Delegate
  • Attribute

Class Type

  • Concrete Class: A class having all its member non-abstract is known as concrete class.
  • Abstract Class: A class is followed by abstract keyword is known as abstract class.
  • Pure Abstract Class: A class having all its member abstract is known as pure abstract class.

Inside Class Theory:- There is no role of any access specifier.

Rule For Outside Class Theory:- To access a member of a class from outside class first we have created object of a class.

General Syntax To Create Object Of Class:

Class name  object name = new Class name();
Example ->
  1. Class1 obj = new Class1();
  2. Class1 obj1;  obj1 = new Class1();
  • Every object have separate memory blocks.
  • In object-oriented programming every class contains its own object or reference or pointer in the same class with name “This” keyword, It behaves like an owner of class.

Difference Between Class And Structure

Constructor

A constructor is a special function having following characteristics:

  • Name of constructor = Name of class.
  • Constructor has no return type
  • It uses to initialize data member of a class.
  • A constructor is a method that is automatically called just after the creation of an object. And destructor is a method that is called just before the destruction of an object.

  • A constructor always returns a reference of the newly created object.
  • In the complete life cycle of the object, only one constructor and one destructor will call.

Types Of Constructors

Default Constructor

A constructor without parameter is known as default constructor.

If we are not creating any constructor in a class then compiler automatically creates a default constructor in the same class that is not visible to see.

Example ->

Class Class1
{
public int a,b;
public Class1()
{
a=5;
b=7;
}
}
Class1 obj1=new Class1();

Parameterized Constructor

A constructor having one or more than one parameter is called as Parameterized constructor.

Example ->

Public Class1(int x, int y)
{
a=x;
b=y;
}
Class1 obj1=new class1();

Copy Constructor

A constructor having one parameter have same class type is known as copy constructor. The copy constructor is using for copying values of one object into a newly created object.

Example ->

Class Class1
{
private Class1()
{
a=5;
b=7;
}
public Class1(int x, int y)
{}
public Class1(Class1 obj)
{
this.a=obj.a;
this.b=obj.b;
}
}
Class1 obj1=new Class1(10,20);
Class1 obj2=new Class1();
obj2=obj1;
Class1 obj3=new Class1(obj1);

Static Constructor

A constructor is followed by a static keyword is understood as a static constructor. It has following limitations:

  • We can not use any access specifier with a static constructor.
  • We can not use any argument within a static function.
  • Static constructor automatically calls once in the complete program whenever we tent to are attempting to access class.
  • A static constructor uses to initialize a static member of a class.

Private Constructor

A constructor followed by private access specifier is understood as a private constructor.

  • It uses to job down singleton pattern programming.
  • The pattern within which making single object is known as singleton pattern programming.

Property

Property is an intelligent data field which uses to hold the reference of data fields.

General Syntax To Write Property:

Access specifier   Return type   Name of property
{
get
}
{
set
}
}}

Example ->

Public int pro_id
{
get
{
return this.id;
}
set
{
this.id=value;
}
}}

Oops Concepts in C-sharp

Inheritance

An inheritance is a technique by which a class can inherit or adopt properties of another class it is also used for data sharing and reusability of code. It is the important concept of c-sharp.

Example ->

namespace inheritence_example
{
class program
{
public static void Main(string[] args)
{
class1 obj1=new class1();
class2 obj2=new class2();
obj1.setdata();
obj2.setdata();
}
class class1
{
private int a;
protected int b;
public int c;
public void setdata()
{
a=3;
b=7;
c=9;
}}
class class2:class1
{
private int d;
protected int e;
public int f;
public void setdata()
{
d=10;
e=20;
f=30;
base.setdata();
}
}
}
}

In case of inheritance when we are creating an object of drive class then the constructor is the root from drive class to the parent class and execute from the parent class to drive class.

Example ->

namespace inheritance
{
class Program
{
static void Main(string[] args)
{
class2 obj1 = new class2();
obj1.setdata();
}
class class1
{
private int a;
protected int b;
public int c;
public void setdata()
{
a = 5;
b = 7;
c = 9;
}
public class1()
{
Console.WriteLine("constructor of parent class call");
}
public class1(int x)
{
}
}
class class2:class1
{
private int d;
protected int e;
public int f;
public void setdata()
{
d = 10;
e = 20;
f = 30;
}
public class2()
{
Console.WriteLine("constructor of drive class call");
}
}
}
}

Output -> 

Constructor of parent class call

Constructor of drive class call

Keyword For Inheritance In C-sharp

: => inherit

Sealed =>Not inheritable

Abstract => Must inherits

Note:

  • Inheritance tells us a parent class variable can hold a reference of its any child class.
  • By default all classes are inheritable.
  • We can not create an object of abstract class we can create the only reference variable of the abstract class.

Polymorphism

The technique by which we can write two or more than two functions with the same name in a single class or in drive class is called as polymorphism. we can achieve polymorphism using two technique.

  1. Function Overloading
  2. Function Overriding

Types Of Polymorphism

  1. Compile-time or Static polymorphism
  2. Runtime or Dynamic polymorphism

1.  Function Overloading (Compile-time Polymorphism)

The technique by which we can write two or more than two functions having the same name and different signature in a single class or in drive class is known as function overloading.

Example ->

namespace function_overloading
{
class Program
{
static void Main(string[] args)
{
All_Area obj1 = new All_Area();
obj1.area(10);
obj1.area(10, 20);
obj1.area(10, 20, 30);
Console.Read();
}
class All_Area
{
public void area(int a)
{
int arr = a * a;
Console.WriteLine("The Area of square is {0}",arr);
}
public void area(int a, int b)
{
int arr = a * b;
Console.WriteLine("Area of rectangle is {0}", arr);
}
public void area(int a,int b,int c)
{
int arr = a * b * c;
Console.WriteLine("Area of cuboid is {0}", arr);
}
}
}
}

Output ->

The Area of square is 100

Area of rectangle is 200

Area of cuboid id 600

Note:

  • The advantage of function overloading is code readability.

2. Function Overriding (Runtime Polymorphism)

The technique in which we can write two or more than two functions having the same name and same signature only in drive class is known as function overriding.

Keyword For Overriding:

Virtual => Overridable

Override => Overrides

Sealed => Not Overridable

Abstract => Must Override

Note:

  • By default, all functions are not overridable.
  • If we are using override keyword with a function than this override keyword also marks the function as virtual.
  • An abstract method must contain in an abstract class.
  • An abstract method has the only signature we can not write a body of the abstract method.
  • If the object and reference are the same class then the method will call for the same class.
  • Overriding used to achieve runtime polymorphism.

Example ->

namespace Function_overriding
{
class Program
{
static void Main(string[] args)
{
parent obj1 = new child();
obj1.AA();
obj1.BB();
obj1.CC();
Console.Read();
}
class parent
{
public virtual void AA()
{
Console.WriteLine("we are in class parent AA");
}
public virtual void BB()
{
Console.WriteLine("we are in the class parent BB");
}
public virtual void CC()
{
Console.WriteLine("we are in class parent CC");
}
}
class child: parent
{
public override void AA()
{
Console.WriteLine("we the class child AA");
}
public new void BB()
{
Console.WriteLine("we are in class child BB");
}
public void CC()
{
Console.WriteLine("class child CC");
}
}
}
}

Output ->

we the class child AA

we are in class parent BB

class parent CC

Condition For Checking Signature Of Functions:

Two functions of the same signature have following condition are as follows.

  • Both have the same name.
  • Both have the same number of argument.
  • All argument have the same data type.
  • We do not consider return type of function to check the signature.

Upcasting

Conversion of child data type into parent data type is known as upcasting. It is automatically executed by the C-sharp compiler.

Example ->

class a
{ }
class b : a
{ }
a obj1 = new a();
b obj2 = new b();
obj1 = obj2;

Downcasting

Conversion of parent data type into child data type is known as downcasting. To execute downcasting process there is always a need to use the typecasting method.

Example ->

class a
{ }
class b : a
{ }

a obj1 = new a();
b obj2 = new b();
obj2 = (b)obj1;

Object

An object is a class that is a most super class or most parent class of .net classes. We can say that all .net types are drives from the object class.

Boxing

Conversion of value type into object type is known as boxing. It is automatically executed to the c-sharp compiler.

Example ->

Object obj;
obj = 25;
obj = "abc";

Unboxing

Conversion of object data type into value data type is known as unboxing. To execute unboxing process there is always a need for type casting method.

Example ->

Object obj=10;
double a =Convert.ToDouble(obj);

Indexer

Indexer provides facilities by which we can use the index like an array with the object.

General Syntax Of Index:

Access Specifier   Return type  this[type index]
{
get{}
set{}
}

Example ->

public int this[int myindex]
{
get { return arr[myindex]; }
set { arr[myindex] = value; }
}

Interface

An interface is an abstract user define the data type. It has the following limitation.

  1. We can not create an object of the interface however, we can create solely a reference variable for the interface.
  2. We can not use any access specifier within an interface. By default all members are public.
  3. We can not define data field within the interface.
  4. We can declare solely function, property, and event within the interface in abstract form without using abstract keyword thus we can write the only signature of function, property, and event within the interface.
  5. A class or interface can inherit only one class at a time but a class or interface can implement one or more interface at the same time.

General Syntax For Interface:

Access specifier   interface   name of interface
{
statement;
}

Example ->

using System;
namespace Interface
{
class Program
{
public static void Main(string[] args)
{
mylogin obj=new myloginlogin();
bool b=obj.login("","");          //class
Ilogin IL1;
IL1=obj;
IL1.login("","");                 //Interface
}
public interface Ilogin
{
bool login(string email, string password);
}
public class mylogin:Ilogin
{
public bool login(string email, string password)
{
bool Ilogin login(string email,string password)
}
}
}
}

Delegate

A delegate may be a special information kind that’s accustom hold address or reference to operating its accustomed produce decision to call back function.

  • The best use of delegate is event handling.
  • We can not handle the event without delegate.

Example ->

using System;
namespace delegate
{
class Program
{
public static void Main(string[] args)
{
}
public void fun1()
{
}
public void fun2()
{
}
public void fun3()
{
}
public void fun4(int x)
{
}
public void fun5(int y)
{
}
public int fun6(int a,int b)
{
}
public int fun7(int x,int y)
{
}
}
}

In the above code fun1, fun2 and fun3 have the same signature,  fun4 and fun5 have the same signature furthermore fun6 and fun7 have the same signature. So we can write one delegate for function fun1, fun2, and fun3, on the other hand, one for both function fun4 and fun5 furthermore one for both fun6 and fun7 respectively.

General Syntax To write Delegate In C-sharp:

Access specifier   delegate   name of delegate inform of function

Example:->

public delegate void mydel1();

Note:

  • mydel1 is a delegate which is capable to hold address or reference of any function having no return type and no argument.

Procedure To Create Object Of Delegate and Hold Function Into It:

public delegate void mydel1();                                                  //create a delegate of fun1, fun2 and fun3
public delegate void mydel2(int x);                                        //create a delegate of fun4 and fun5
public delegate int mydel3(int x, int y);                               //create a delegate of fun6 and fun7
mydel1 obj=new mydel1();                                                        //create object of mydel1 delegate
mydel2 obj=new mydel2(fun5);                                           //create object of mydel2 delegate
mydel3 obj1=new mydel3(fun6);                                        //create object of mydel3 delegate

Note:

  • In the above code, mydel1 is a delegate which can hold the function fun1,fun2, and fun3.
  • mydel2 is a delegate which can hold the function fun4 and fun5.
  • mydel3 is a delegate which can hold the function fun6 and fun7.

Procedure To Call Function Using Delegate Object In C-sharp:

As we call function like  fun1(); same as delegate object call function

obj(); =>fun1()
obj=fun2;
obj(); => fun2();       //call
obj2(); => fun5()
obj2=fun5
obj2(); => fun5();      //call
obj1(int x);
obj1=fun5;
obj1(int y);
int a=obj1(10);                    // call for function fun5
int a=obj2(2,2);                  // call for function fun7

Example ->

public string fun11(float a , string b)
{
}

Create delegate for the above function fun11 and moreover create an object and call a function with a delegate object.

public delegate string mydelfun11(float a, string b);                             //delegate
mydelfun11 obj= new mydel11(fun11);                                                  // object of delegate
string ss=obj(3.5f,"ABC");                                                                           // call a function

Multicast Delegate

Multicast delegate provides facilities through that a delegate object will hold two or more than two functions and furthermore call using the equivalent object.

We can use the following operation with a multicast delegate.

+=
-=
+

Example ->

mydel obj = new mydel(fun1);
obj();   => fun1();      //call fun1
obj+=obj2;
obj();                   //call fun1 and fun2

Covariance

Covariance provides facilities by that a delegate object will hold a function having the equivalent return type, in fact, its child return type.

Example ->

namespace @delegate
{
class Program
{
public delegate director mydel();
static void Main(string[] args)
{
mydel obj1 = new mydel(fun1);                    //error
mydel onj2 = new mydel(fun2);                  //due to delegate object have same type of function
mydel obj3 = new mydel(fun3);                 //due to covariance
}
public class manager
{
}
public class director : manager
{
}
public class technicion : director
{
}
public static manager fun1()
{
return new manager();
}
public static director fun2()
{
return new director();
}
public static technicion fun3()
{
return new technicion();
}
}
}

Contravariance

It provides facilities by that a delegate object will hold a operate having a similar type of argument, in fact, its parent type.

Example ->

namespace example
{
class Program
{
public delegate middle mydel1(middle m);
public static void Main(string[] args)
{
mydel1 obj1=new myde1l(fun1);                                        //call due to contravariance
mydel1 obj2=new mydel1(fun2);                                      //call due to  delegate object has same type of function
mydel1 obj3=new mydel1(fun3);                                      //Error
}
public    class top
{
}
public    class middle: top
{
}
public    class bottom: middle
{
}
public static middle fun1(top m)
{
return null;
}
public static middle fun2(middle m)
{
return null;
}
public static bottom fun3(bottom b)
{
return null;
}
}
}

Conclusion

C-sharp is a very important language that’s a helpful addition to Microsoft .net framework. C# provides the standard options of Oops inheritance, interface implementation, polymorphism, encapsulation what is more C# includes a fashionable set of statements that use to regulate the flow of execution in your code.

You have got any question associated with a subject??? Please place your queries in the comments section below. I will retreat to answer your queries as before long as attainable!

3 thoughts on “Introduction to C-sharp.Net Basics”

Leave a Comment