Skip to main content

Unit-1 Introduction to C#.NET (Class 12)

 


What is .NET Framework?

The .NET Framework is a software development platform developed by Microsoft.
It provides tools and libraries to build and run Windows applications, web services, and web apps.

It gives tools and libraries that make it easier to write programs. It also helps the computer run those programs safely and efficiently.

Microsoft started working on .NET Framework in the late 1990s.
The first version, .NET Framework 1.0, was released in 2002.

 

The .NET Framework is made up of several important components:

1.      Common Language Runtime (CLR) is the core engine that runs your program. It converts your code into machine code that the computer can understand, manages memory, handles errors, and ensures that your program runs safely.

2.      The Class Library is a large collection of ready-made code that helps you perform common tasks like working with files, databases, graphics, the internet, and more. It saves you from writing everything from scratch.

3.      The languages supported by .NET Framework include C#, VB.NET, F#, and others. You can write your program in any of these languages, and the .NET Framework will run it.

4.      ASP.NET is a part of the .NET Framework that allows you to build websites and web  applications.

5.      Windows Forms and WPF (Windows Presentation Foundation) are used to build desktop applications for Windows, with user interfaces like buttons, windows, and graphics.

Introduce C#, its features and applications

C# is pronounced "C-Sharp".

  It is an object-oriented programming language created by Microsoft that runs on the .NET Framework.

C# has roots from the C family, and the language is close to other popular languages like C++ and Java.

The first version was released in year 2002. The latest version of C# is C# 14, which was released alongside .NET 10 in November 2025.

C# is used for:

        Mobile applications

        Desktop applications

        Web applications

        Web services

        Web sites

        Games

        VR

         Database applications

        And much, much more

Reasons to use c#

        It is one of the most popular programming language in the world

        It is easy to learn and simple to use

        It has a huge community support

        C# is an object oriented language which gives a clear structure to programs and allows code to be reused, lowering development costs

        As C# is close to C, C++ and Java, it makes it easy for programmers to switch to C# or vice versa 

Structure of C#


using System;             // Gives access to Console and other basic features 
namespace MyFirstProgram      // Defines a namespace
{
    class Program             // Main class of the program
    {
        static void Main(string[] args)  // Main method, starting point
        {
            Console.WriteLine("Hello, World!"); // Prints output to screen
        }
    }
}

  •  using System;

This line allows your program to use predefined classes and functions in the System namespace.
For example,
Console.WriteLine() is part of System.

It's like importing built-in tools so you can use them in your code.

  •  namespace MyFirstProgram

A namespace is like a container for your classes. It helps organize code and avoid naming conflicts when your program gets bigger.

 Think of it like putting files in folders.

  •  class Program

All C# code must be written inside a class. Here, the class is named Program.
A class is a blueprint that contains data (variables) and actions (methods or functions).

It's like a box that holds your actual code logic.

  •  static void Main(string[] args)

This is the entry point of the C# program. When you run the program, execution starts from this method.

·         static: You don’t need to create an object to run this method.

·         void: This means the method doesn’t return any value.

·         Main: The name of the method where the program starts.

·         string[] args: This is used to take command-line arguments (optional for now).

 It tells the computer: "Start here!"

  •  Console.WriteLine("Hello, World!");

This line displays text on the screen.

·         Console: A built-in class for input/output.

·         WriteLine(): A method that prints the given text and moves to a new line.

This is the actual action of your program.

C# Variables

A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable's memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable.

Rules to create variable name:

        The name can contain letters, digits, and the underscore character (_).

        The first character of the name must be a letter or underscore. C# is case-sensitive;

        C# keywords can't be used as variable names.

        Syntax: type variableName = value;

Example   string name = "Rohit";

1.4 C# identifier

In programming languages, identifiers are used for identification purposes. Or in other w  ords, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. 

Rules for defining identifiers in C#:
 

        The only allowed characters for identifiers are all alphanumeric characters([A-Z][a-z][0-9]), ‘_‘ (underscore).

        Identifiers should not start with digits([0-9

        Identifiers should not contain white spaces.
 

        Identifiers are not allowed to use as keywords unless they include @ as a prefix. For example, @as is a valid identifier, but “as” is not because it is a keyword.

        C# identifiers allow Unicode Characters.

        C# identifiers are case-sensitive.

        C# identifiers cannot contain more than 512 characters.

        Identifiers do not contain two consecutive underscores in their name because such types of identifiers are used for the implementation.

C# Keywords

Keywords are predefined sets of reserved words that have special meaning in a program. The meaning of keywords can not be changed, neither can they be directly used as identifiers in a program. C# has a total of 79 keywords. All these keywords are in lowercase

Abstract

As

Base

Bool

Break

Byte

Case

Catch

Char

Checked

Class

Const

Continue

Decimal

Default

Delegate

Do

Double

Else

Enum

Event

Explicit

Extern

False

Finally

Fixed

Float

For

Foreach

Goto

If

Implicit

In

in (generic modifier)

Int

Interface

Internal

Is

Lock

Long

Namespace

New

Null

Object

Operator

Out

out (generic modifier)

Override

Params

Private

Protected

Public

Readonly

Ref

Return

Sbyte

Sealed

Short

Sizeof

Stackalloc

Static

String

Struct

Switch

This

Throw

True

Try

Typeof

Uint

Ulong

unchecked

Unsafe

Ushort

Using

using static

Void

Volatile

While

 

Keywords can be used as identifiers if @ is added as prefix.

 

 


C# - Data Types

Data types specify the type of data that a valid C# variable can hold. C# is a strongly-typed language. It means we must declare the type of a variable that indicates the kind of values it is going to store, such as integer, float, decimal, text, etc.

There are 3 types of data types in C# language.

Types

Data Types

Value Data Type

short, int, char, float, double, struct, enum, null

Reference Data Type

String, Class, Object,  Interface and array

Pointer Data Type

Pointers

 

 1. Value Data Types

Value data types hold the actual data in memory. These types include numbers, characters, booleans, and so on. C# supports both signed and unsigned versions of numeric types.

There are two kinds of value data types:

🔸 a) Predefined Data Types:

These are built-in types like int, char, float, double, etc. Here is a table showing their memory size and value range

 

Data Type

Size in C#

Value Range

Notes

byte

1 byte

0 to 255

Unsigned integer

sbyte

1 byte

-128 to 127

Signed integer

short

2 bytes

-32,768 to 32,767

Signed 16-bit integer

ushort

2 bytes

0 to 65,535

Unsigned 16-bit integer

int

4 bytes

-2,147,483,648 to 2,147,483,647

Signed 32-bit integer

uint

4 bytes

0 to 4,294,967,295

Unsigned 32-bit integer

long

8 bytes

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Signed 64-bit integer

ulong

8 bytes

0 to 18,446,744,073,709,551,615

Unsigned 64-bit integer

float

4 bytes

±1.5×10⁻⁴⁵ to ±3.4×10³⁸

Single-precision, ~7 digits accuracy

double

8 bytes

±5.0×10⁻³²⁴ to ±1.7×10³⁰⁸

Double-precision, ~15–16 digits accuracy

decimal

16 bytes

±1.0×10⁻²⁸ to ±7.9×10²⁸

High precision (28–29 digits), good for financial calculations

char

2 bytes

Unicode U+0000 to U+FFFF

Stores a single Unicode character

bool

1 byte (typically)

true or false

Represents Boolean values; size depends on implementation

int myNum = 5;               // Integer (whole number)

double myDoubleNum = 5.99D;  // Floating point number

char myLetter = 'D';         // Character

bool myBool = true;          // Boolean

string myText = "Hello";     // String

2) User defined Data Types

Structure: Structure (struct) is a value type that groups related variables. It can also include methods and constructors.

struct Coordinate

 {

public int x;

public int y;

}

enum: enum (or enumeration type) is used to assign constant names to a group of numeric integer values. It makes constant values more readable, for example, WeekDays.Monday is more readable then number 0 when referring to the day in a week.

An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant names can be declared inside the curly brackets and separated by a comma.

Example:

enum WeekDays

{

Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

 }



2. Reference Data Types

Reference types don’t store the actual data but store a reference (or address) pointing to the data's memory location. If you change the value using one variable, the change reflects in all references to that data.

There are two categories:

  • Predefined types: like string, object

  • User-defined types: like class, interface

Example:

string myText = "Hello"; // Reference type

Pointer Data Type in C#

A pointer is a variable that stores the memory address

of another variable.

It is often called a locator because it points to a location in

memory.

Pointers can only be used in an unsafe context in C#.

Symbols Used in Pointers

Symbol

Name

Description

&       

Address-of operator               

     Gets the memory address of a variable.

*

Indirection (dereference) operator       

     Accesses the value stored at a memory              address.

Declaring a Pointer

A pointer is declared using the * symbol after the data type.

int *a;   // Pointer to an integer

char * c;   // Pointer to a character

C# - Type Conversion (Type Casting)

Type conversion in C# means converting one data type into another. It is also known as type casting. There are mainly two types of type casting in C#: implicit type conversion and explicit type conversion.

1. Implicit Type Conversion

Implicit type conversion is automatically handled by the C# compiler. It happens when the conversion is safe and no data will be lost. This usually includes conversions from a smaller data type to a larger data type, such as from int to double, or from a derived class to a base class.

For example:
If we have an integer variable
a with value 9, and we assign it to a double variable b, the compiler automatically converts it.

int a = 9;

double b = a;

Here, a is automatically converted to a double type. When we print both values using Console.WriteLine, the output will be:

9

9

 

2. Explicit Type Conversion

Explicit type conversion, also known as manual casting, is done by the programmer using a cast operator. This type of conversion is used when converting from a larger data type to a smaller one, or between incompatible types. It can result in data loss.

For example:
If we have a double variable
a with value 9.78, and we want to convert it into an integer:

double a = 9.78;

int b = (int)a;

In this case, we are manually converting a to an integer by placing (int) in front of the variable. This will cut off the decimal part and the output will be:

9.78

9

 

3. Type Conversion Using Built-in Methods

C# provides a set of built-in methods in the Convert class for converting between data types. These methods allow safe and easy conversions between types like int, string, double, boolean, etc.

Some commonly used methods include:

  • Convert.ToString() – converts any value to a string.
  • Convert.ToDouble() – converts a value to a double.
  • Convert.ToInt32() – converts a value to a 32-bit integer.
  • Convert.ToBoolean() – converts a value to a boolean.

Example:

int a = 20;

double e = 2.25;

bool b = true;

 

Console.WriteLine(Convert.ToString(a));     // Outputs "20"

Console.WriteLine(Convert.ToDouble(a));     // Outputs 20.0

Console.WriteLine(Convert.ToInt32(e));      // Outputs 2

Console.WriteLine(Convert.ToString(b));     // Outputs "True"

 

 4. Using Int32.Parse() Method

In addition to the Convert class, we can also use the Parse() method to convert a string to an integer. The string must be a valid numeric value.

Example:

string input = "123";

int result = Int32.Parse(input);

Console.WriteLine(result);

This will convert the string "123" into the integer 123.

 

Complete Program Example: Converting int to String and Double

Here is a complete program that demonstrates how to convert an integer into a string and a double using the Convert class.

using System;

 

namespace Conversion {

  class Program {

    static void Main(string[] args) {

 

      int num = 100;

      Console.WriteLine("int value: " + num);

 

      // Convert int to string

      string str = Convert.ToString(num);

      Console.WriteLine("string value: " + str);

 

      // Convert int to double

      double doubleNum = Convert.ToDouble(num);

      Console.WriteLine("Double value: " + doubleNum);

    }

  }

}

Output:

int value: 100 

string value: 100 

Double value: 100 

Comments

Popular posts from this blog

Introduction to Software Engineering (Class 12)

 Introduction to Software Engineering Software engineering is the branch of computer science that deals with the design, development, testing, and maintenance of software applications. Software engineers apply engineering principles and knowledge of programming languages to build software solutions for end users. IEEE, in its standard 610.12-1990, defines software engineering as the application of a systematic, disciplined, which is a computable approach for the development, operation, and maintenance of software. Boehm defines software engineering, which involves, ‘the practical application of scientific knowledge to the creative design and building of computer programs. It also includes associated documentation needed for developing, operating, and maintaining them.’ Importance of Software Engineering Reduces complexity Large software systems are divided into smaller, manageable modules. Each module is developed and solved independently, ...