Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, 26 April 2021

Let’s play with inheritance. What will be outputted?

class A


public void abc(int q)


Console.WriteLine("abc from A");



class B : A


public void abc(double p)


Console.WriteLine("abc from B");



static void Main(string[] args)


int i = 5;

B b = new B();;



Answer :-

abc from B.

A typical polymorphism understanding swindle. The main thing is not to forget and overlook anything. What will be the result of execution of the following code?

class Program


static void Main(string[] args)


MyClassB b = new MyClassB();

MyClassA a = b;;




class MyClassA


public MyClassA()


Console.WriteLine("constructor A");


public void abc()





class MyClassB:MyClassA


public MyClassB()


Console.WriteLine("constructor B");


public void abc()





Answer :-

constructor A
constructor B

During initialization of the B class, the constructor of the A class will be executed by default, then constructor of the B class. After assignment of the b value to a type variable of the A class, we will get an instance of the B class in it. One would think that abc() from the B class should be called, but since there is no specification of any predicate of the abc method in the B class, it hides abc from the A class. The example is not quite correct and abc() in the B class will be underlined, since the new predicate is required.

Monday, 29 March 2021

Constructors Part 4 | Static Constructors vs Non Static Constructor |

Static Constructors Vs Non-Static Constructors :

If a constructor is explicitly declared by using a static modifier we call that constructor as static constructor whereas rest of other are non-static constructor only.

Constructors are responsible for initializing fields/variables of a class, static fields are initialized by static constructors and non-static fields are initialized by non-static constructors.

Static constructors are implicitly called whereas non-static constructors must be explicitly called.

Static constructors executes immediately once the execution of a class starts and more over it's the first block of code to run under a class whereas non-static constructors executes only after creating the instance of class as well as each and every time the instance of class is created.

In the life cycle of a class static constructors executes one and only one time whereas non-static constructors executes for zero times if no instances are created and "n" times if "n" instances are created.

 Non-static constructors can be parameterized but static constructors can't have any parameters because  static constructors are implicitly called and more over it's the first block of code to run under class.

Non-static constructors can be overloaded where as static constructors can't be overloaded.

 // C# Program to demonstrate
// how to declare the static
// constructor and non-static
// constructor
using System;
class ABC{
// Static variable
static int s;
// Non-static variable
int ns;
// Declaration of
// static constructor
static ABC()
    Console.WriteLine("It is static constructor");
// Declaration of
// non-static constructor
public ABC()
    Console.WriteLine("It is non-static constructor");
// Main Method
static void Main(string[] args)
    // Static constructor will call implicitly
    // as soon as the class start to execute
    // the first block of code to execute
    // will be static constructor
    // Calling non-static constructor
    ABC obj1 = new ABC();

Output :

It is static constructor
It is non-static constructor

Every class contains an implicit constructor if not defined explicitly and those implicit constructors are defined based on the following criteria :

⟶ Every class except a static class contains an implicit non-static constructor if not defined with an explicit constructors .

⟶ Static constructors are implicitly defined only if that class contains any static fields or else that constructor will not be present at all.

Thursday, 25 March 2021

Constructors in C#.NET Part 3 | Why Constructors are Needed in our class | Clcoding

Every class requires a constructor to be present init if we want to create the instance of that class.

Every class contains an implicit constructor if not defined explicitly and with the help of that implicit constructor instance of class can be created.

What is the need of defining a constructor explicitly again ?

Implicit constructor of a class will initialize variables of a class with the same value even if we create multiple instance of that class.

If we define constructor explicitly with parameters then we get a change of initializing the fields or variables of the class with a new value every time we are going to create instance of that class.

When ever we define a class identify whether if the class variables requires some values to execute and if they are required then define a constructor explicitly and pass values thru that constructor , so that every time the instance of the class is created we get a chance of passing new values.

Note : Generally every class requires some values for execution and the values that is required for a class to execute are always sent to that class by using the constructor only.

Tuesday, 23 March 2021

Types of Constructors in C#.NET Part 2 | C#.NET Tutorial | Clcoding

Type of Constructors 

1. Default or Parameter Less Constructor

2. Parameterized Constructor

3. Copy Constructor

4. Static Constructor

Default or Parameter Less Constructor : 

 If a constructor method doesn't take any parameters then we call that as default or parameter less. these constructors can be defined by a programmer explicitly or else will be default implicitly provided there is no explicit constructor under the class. 

class Test


public Test ()     //Implicit Constructor




Parameterized Constructor :

If a constructor method is defined with out any parameters we call that as parameterized constructor and these constructor can be defined by the programmers only but never can be defined implicitly.

Copy Constructor :

If we want to create multiple instances with the same values then we use these copy constructors, in a copy constructors the  constructors takes the same class as a parameters to it.

Static Constructor :

If a constructor is explicitly declared by using static modifier we call that as static constructor. All the constructors we have defined till now are non-static or instance constructors.

Class Test


static Test ()    //Static constructor defined explicitly



public Test ()   // Implicit default constructor




If a class contains any static the only implicit static constructors will be present or else we need to define them explicitly whereas non-static constructors will be implicitly defined in every class (except static class) provided we did not define them explicitly.

Static constructor are responsible in initializing static variables and these constructors are never called explicitly they are implicitly called and more over these constructor are first to execute under any class.

Static constructors can't be parameterized so overloading static constructors is not possible. 


Monday, 22 March 2021

Constructors in C#.NET Part 1 | C#.NET Tutorial | Clcoding

It's a special method present under a class responsible for initializing the variable of that class.

The name of a constructor method is exactly the same name of the class in which it was present and more over it's a non-value returning method.

Each and every class requires this constructor if we want to create the instance of that class.

class Test


int i ;

Test obj = new Test();  // Valid

⟶ It's the responsibility of a programmer to define a constructor under his class and if he fails to do so, on behalf of the programmer an implicit constructor gets defined in that class by the compiler.

 class Test


int i ; string s; bool b;

public Test ()


i = 0;  // Initializing the variables

s = null;

b = false;



Implicitly defined constructors are parameter less and these constructor are also known as default constructors.

Implicitly defined co0nstructor are public.

We can also defined a constructor under the class and if we define it we can call it as explicit constructor and explicit constructor can be parameter less or parameterized also.

[ < modifiers > ] <Name> ( [ < parameter list > ] )



Defining : Implicit or Explicit

Calling : Explicit

Tuesday, 26 January 2021

Namespace and Main Method in C# PROGRAMMING | Clcoding

What is Namespace ?

A namespace is used to organize your code and is collection of classes, interfaces, structs, enums and delegates.

To define a namespace in C#, we will use the namespace keyword followed by the name of the namespace and curly braces containing the body of the namespace as follows: 


namespace name_of_namespace {

// Namespace (Nested Namespaces)
// Classes
// Interfaces
// Structures
// Delegates


Note : 

  • If you don't want to use namespace you can use fully qualified name (FQN).
  • Two classes with the same name can be created inside 2 different namespaces in a single program.
  • Inside a namespace, no two classes can have the same name.
  • In C#, the full name of the class starts from its namespace name followed by dot(.) operator and the class name, which is termed as the fully qualified name of the class.

Example :

 // defining the namespace name1
namespace name1

    // C1 is the class in the namespace name1
    class C1
         // class code

What is main Method ? 

Main method is the entry point into your application. 

What is CLR (Common Language Runtime) in DotNET (.Net) Framework | Clcoding

The CLR (Common Language RunTime) :

  • Is the foundation of the .NET Framework.
  • Acts as an execution engine for the .NET Framework.
  • Manages the execution of programs and provides a suitable environment for programs to run.
  • Provides a multi-language execution environment.

The following figure shows a more detailed look at the working of the CLR:


Just in time (JIT) compiler converts MSIL to native code, which is CPU specific code.

When a code is executed for the first time;

⟶ The CIL ( COMMON INTERMEDIATE LANGUAGE ) code is converted to a code native to the operating system.

⟶ This is done at runtime by the Just-In-Time (JIT) compiler present in the CLR.

⟶ The CLR converts the CIL code to the machine language code.

⟶ Once this is done, the code can be directly executed by the CPU.

Thursday, 7 May 2020

if – if else – if else if ladder in C#

C#  IF Statement :

Syntax :

//code to be executed

Prog : Write a program to test whether a number is even or not

C#  IF-ELSE Statement :

Syntax :

// code to be executed
//Code if condition is false

Prog : Write a program to test whether a number is even or not

C# IF - ELSE - IF LADDER Statement :

Syntax :

//code to be executed
}else if (Condition2)
//code to be executed
//Code when all the condition are false

Program : Write a program to print Grade of Student as per Marks.

Sunday, 3 May 2020

Integer and Decimal value in C#

In this blog, we will discuss about
  • C# Integer Datatypes
  • C# Decimal Datatypes
C# Integer Datatypes :
  • We can only store whole number in them
  • Whole Number mean number without any decimal such as 34, 204, 35 etc
List of C# Integer Datatypes
  • byte
  • short
  • int
  • long
 The difference between all these integer datatype how big value we can store in it.

- Byte 
  • sbyte can store value between -128 and 127.
  • byte can store value between 0 and 255.
- Shot 
  • short can store between -32768 and 32767.
  • ushort can store between 0 and 65535.
- Int 
  • int can stor value between -2147483648 and 2147483647.
  • uint can store value between0 and 4294967295.
- Long 
  • long can store value between -9223372036854775808 and 9223372036854775807.
  • ulong can store value between 0 and 18446744073709551615.
How to declare variables in C#
- General Syntax
      DataType VariableName = value;

  • sbyte scoreSbyte;
  • byte ageByte;
  • short studentIdShort = 5;
  • int studentIdInteger = 43;
Design Example :
- What will be datatype for Age?

Human can live these days at most, 120. Make it 150. so byte is best.
But In older days from history, we find that many people lived for around 1000 years, so Short is best.
What if the Age is relating to Historical Site or Historical Events (Big Bang), which may be thousands or million of years old then int is best.

C# Decimal Datatypes:
  • We can store decimal numbers in them
  • We can also store Whole number in them
  • For example , 34.5, 204.3, 35.7 etc.
List of C# Decimal Datatypes
  • float
  • double
The difference between all these decimal datatype how big value we can store in it.

  • float salaryFloat = 101.5f;
  • decimal salaryDecimal = 101.25m;
  • double salaryDouble = 101.5;

Sunday, 26 April 2020

What is Variable, Datatype and C# Datatypes

Variables :
  • It is location in memory (RAM) to temporary store a value during execution of Program / Software.
  • As the name suggests, the value of variable can change during execution of program.
  • A variable is made of 3 things.
                - Datatype
                - Name
                - Value (optional)

DataType :
   - It means what type of value we can store in the variable.
  • Numbers such 123, 10.50 etc
  • Strings such as UK, Canada etc
  • Data such 11/4/2020

Name :
  • It identifies the variable. For example, firstName, lastName, age
Value :
  • What actual value is stored in variable.
List of DataTypes in C# :
  1. Integer → Numbers such as 120, 345 etc
  2. Decimal → Number such as 3.5, 4.5 etc
  3. Char → Single character such as A,a,B,z
  4. Bool → True or False value
  5. String → More than one characters value such as John Smith, Mark Smith, Canada , Landon etc.
Syntax :
              DataType variableName = value;

Naming Rules :
 一  Names may consists of letters (A-Z or a-z), digits and underscores
  •  e.g firstName, firstNumber, _Age, firstNumber1 (correct)
  • #FirstName, @Email (wrong)

一 Name must begin with a letter or underscore.
  • e.g. firstName or _firstName (correct)
  • 1stName or 2ndNumber (wrong)
一Name must not have spaces.
  • e.g. first Name (wrong)
  • firstName (correct)
一 Names must not have reserved word or C# keywords. List of C# keyword are shown in the table.

Naming Conventions :
ー Names must be meaningful
  • e.g. firstNameString, cnicString, studentldlnteger (correct)
  • a,b, obj1 (wrong)
ー  Name must include its datatype
  • e.g. firstNameString, cnicString, studentldlnteger (correct)
  • FirstName, firstname, CNIC or Studentld (wrong)
ー Name must begin with lowercase letter and then capitalize each word of the name
  • e.g. firstNameString, cnicString, studentldinteger (correct)
  • FirstName, firstname, CNIC or StudentId (wrong)

Introduction of C# | First C# Program | C# Vs Java |

C# Introduction 
  • Computer Programming : It is a process of creating computer programs or in other word software to solve particular problem. 
               - Word Processor
               - Hospital Management
               - Spreadsheet
  • It is a Object Oriented Programming Language from Microsoft Corporation in year 2000.
  • C# is a part of .Net Framework and currently in version 16.3.
  • It is helps in developing variety of application such as windows based, Database based, Web Services, Web Application and more. 
C# Vs Java : Both are Object Oriented

C# - 
  1. Required CLR (Common Language RunTime)
  2. GOTO Statement is present
  3. Support Structure and Unions
  4. Support Unchecked Exception
  5. Less Secure than Java
Java -
  1. Required JRE (Java Runtime Environment)
  2. No GOTO statement 
  3. No Structure and Union
  4. Supports both Checked and Unchecked Exception
  5. Much Secure
First C# Program :-

Popular Posts


Android (23) AngularJS (1) Assembly Language (2) Books (10) C (75) C# (12) C++ (81) Course (1) Data Strucures (4) Downloads (1) Engineering (13) flutter (1) FPL (17) Hadoop (1) HTML&CSS (38) IS (25) Java (87) Leet Code (4) PHP (20) Projects (10) Python (231) R (69) Selenium Webdriver (2) Software (14) SQL (27)