GST Billing Software

header image

C# Introduction

Erachana Line

1.1 What is C#

C# is a simple, modern, general-purpose, object-oriented programming language.

C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows use of various high-level languages on different computer platforms and architectures.

1.2 The .NET Framework

The .Net framework is a revolutionary platform that helps you to write the following types of applications −

  • Windows applications
  • Web applications
  • Web services

The .Net framework applications are multi-platform applications. The framework has been designed in such a way that it can be used from any of the following languages: C#, C++, Visual Basic, Jscript, COBOL, etc. All these languages can access the framework as well as communicate with each other.

The .Net framework consists of an enormous library of codes used by the client languages such as C#.

1.3 C# Programming Structure

A C# program consists of the following parts −

  • Namespace declaration
  • A class
  • Class methods
  • Class attributes
  • A Main method
  • Statements and Expressions
  • Comments

Example:

using System;

namespace HelloworldApplication

{

class Helloworld

{

static void Main(string[] args)

{

/*Helloworld program*/

Console.WriteLine("Hello World");

Console.ReadKey();

}

}

}

When this code is compiled and executed, it produces the following result −

Hello World

Let us look at the various parts of the given program −

  • The first line of the program using System ; - the using keyword is used to include the System namespace in the program. A program generally has multiple using statements.
  • The next line has the namespace declaration. Anamespace is a collection of classes. TheHelloWorldApplication namespace contains the class HelloWorld.
  • The next line has a class declaration, the class HelloWorld contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behavior of the class. However, the HelloWorld class has only one method Main.
  • The next line defines the Main method, which is theentry point for all C# programs. The Main method states what the class does when executed.

• The next line /*...*/ is ignored by the compiler and it is put to add comments in the program.

  • The Main method specifies its behavior with the statement Console.WriteLine("Hello World");

WriteLine is a method of the Console class defined in the System namespace. This statement causes the message "Hello, World" to be displayed on the screen.

  • The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.

Rules:

  • C# is case sensitive.

• All statements and expression must end with a semicolon (;).

• The program execution starts at the Main method.

• Class name and filename should be same.

1.4 Data Types in C#

There are 3 Data Types in C#

• Value types

• Reference types

• Pointer types

Value Type

Value type variables can be assigned a value directly. They are derived from the class System.ValueType.

The value types directly contain data. Some examples are int, char, and float, which stores numbers, alphabets, and floating-point numbers, respectively. When you declare an int type, the system allocates memory to store the value.

Reference Type

The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables.

In other words, they refer to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value.

Example of built-in reference types are:object, dynamic, and string.

Object Type

The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class. The object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs type conversion.

When a value type is converted to object type, it is called boxing and on the other hand, when an object type is converted to a value type, it is called unboxing.

Example: Object obj;

Obj=100; //this is boxing

Dynamic Type

You can store any type of value in the dynamic data type variable. Type checking for these types of variables takes place at run-time.

Syntax for declaring a dynamic type is -

Dynamic <variable name>=value;

Example: dynamic d=20;

Dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at run time.

String Type

The String Type allows you to assign any string values to a variable. The string type is an alias for the System. String class. It is derived from object type. The value for a string type can be assigned using string literals in two forms: quoted and @quoted.

For example: String str="Hello";

A @quoted string literal looks as follows -

@"Hello";

Pointer Type

Pointer type variables store the memory address of another type. Pointers in C# have the same capabilities as the pointers in C or C++.

Syntax for declaring a pointer type is -

type* identifier;

Example: char* cptr;

1.5 Type Conversion

  • Implicit type conversion − These conversions are performed by C# in a type-safe manner. For example, are conversions from smaller to larger integral types and conversions from derived classes to base classes.

Example: using System;

namespace TypeConversionApplication

{

class ExplicitConversion

{

static void Main(string[] args)

{

float a=20.98;

// Explicit conversion from double to int.

Convert.ToInt16(a);

Console.WriteLine(a);

Console.ReadKey();

}

}

}

  • Explicit type conversion − These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.

Example :

using System;

namespace TypeConversionApplication

{

class ExplicitConversion

{

static void Main(string[] args)

{

double d = 5673.74;

int i;

// Explicit conversion from double to int.

i = (int)d;

Console.WriteLine(i);

Console.ReadKey();

}

}

}

C# Type Conversion Methods

SI.No.

Methods & Description

1

ToBoolean

Converts a type to a Boolean value, where possible.

2

ToByte

Converts a type to a byte.

3

ToChar

Converts a type to a single Unicode character, where possible.

4

ToDateTime

Converts a type (integer or string type) to date-time structures.

5

ToDecimal

Converts a floating point or integer type to a decimal type.

6

ToDouble

Converts a type to a double type.

7

ToInt16

Converts a type to a 16-bit integer.

8

ToInt64

Converts a type to a 64-bit integer.

9

ToSbyte

Converts a type to a signed byte type.

10

ToSingle

Converts a type to a small floating point number.

11

ToString

Converts a type to a string.

12

ToType

Converts a type to a specified type.

13

ToUInt16

Converts a type to an unsigned int type.

14

ToUInt32

Converts a type to an unsigned long type.

15

ToUInt64

Converts a type to an unsigned big integer.