Thursday, April 28, 2016

Decimal to int Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 decimal a = 10.0M;
 int b = Convert.ToInt32(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output : 10

Decimal to Float Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 decimal a = 20.0M;
 float b = Convert.ToSingle(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output : 20

Wednesday, April 27, 2016

int to Float Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 int a = 40;
 float b = Convert.ToSingle(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output : 40

int to String Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 int a = 66;
 string b = Convert.ToString(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }  
 }
}
Output : 66

Tuesday, April 26, 2016

int to Decimal Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 int a = 20;
 decimal b = Convert.ToDecimal(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output : 20
 int to Double Datatype
using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 int a = 30;
 double b = Convert.ToDouble(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output : 30

Monday, April 25, 2016

int to Char Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 int a = 66;
 char b = Convert.ToChar(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output :DB

Sunday, April 24, 2016

int to Boolean Datatype Conversion

The conversion of one data type into another data type is the Type Conversion or Type Casting. Let us look at some examples:

int to Boolean Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 int a = 0;
 bool b = Convert.ToBoolean(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
} 

Output : False

Thursday, April 21, 2016

Explicit Conversion

In this conversion it requires cast operator. The casting is required when the information is lost or the conversion or the conversion is not succeeded. It converts integer values into higher or lower range, converting base class instance to derived class. Let us have a look on example:
using System;
namespace ConsoleApplication1
{
 class Explicit 
 {
 static void Main (string [] args) 
 {
 double d = 10.50; 
 int i;
 
 // casting double to int.
 i = (int) d;
 Console.WriteLine (i);
 Console.ReadKey ();
 }
 }
}
Output : 10

Wednesday, April 20, 2016

Type Casting

The type casting is the type conversion of one data type into another data type. In C# there are two types of type casting:

Implicit Conversion

In this conversion no special syntax is required because this conversion is type safe and no data will be lost. For example, Convert small to large integer types, converting from derived class to base class
using System;
class A
{
 public int a;
 public static implicit operator B (A m)
 {
 B w = new B ( );
 w.a = m.a * 2;
 return w;
 }
}

class B
{
 public int a;
 public static implicit operator A (B w)
 {
 A m = new A ( );
 m.a = w.a / 2;
 return m;
 }
}

class Program
{
 static void Main ( )
 {
 A m = new A ( );
 m.a = 10;
 Console.WriteLine (m.a);

// Implicit conversion from A to B.
 B w = m;
 Console.WriteLine (w.a);

// Implicit conversion from B to A.
 A m2 = w;
 Console.WriteLine (m2.a);
 }
}
Output:
10
20
10

Tuesday, April 19, 2016

C# Datatypes - Datatype Conversion, Boxing & Unboxing

Since C# is a strongly typed language, each variable used in the program should have a type and the data types are declared when we declare the variable. The C# data types are categorized into three types:
  • Value Types:
    • Predefined – int, char, short
    • User-defined types – Structure, Enumerations
  • Reference Types : 
    • Predefined – Objects, Strings
    • User-defined types – Arrays, Classes, Interfaces
  • Pointer Types
  • The variables which are based on value types they directly contains values. But the reference type variables differ from value types as they contain the reference of the object. The value types are derived implicitly from System.ValueType whereas the reference types cannot derive new type from the value type.
    The value type contains the null value and each value type has the implicit default constructor that initialize the default value of that type. Like
    int a = new int ( ); which is same as int a = 0;

Monday, April 18, 2016

Common Language Runtime Architecture

  • Class Loader: In this component the compiler of the .Net Framework compiles the source code into intermediate code and it consists of MSIL code and Metadata and they are contained in a portable executable (PE) file.
  • Code Manager: This component manages the code during execution time. It is the responsibility of the code manager to allocate memory to the objects.
  • Garbage collector: This component provides the automatic garbage collection of the object when the object is no longer in use. 
  • Security Checker: It is the most important component of CLR. The responsibility is to restrict the access to system resources such as hard disk.
  • Type Checker: This component provides the facility of data type checking of the variable. It also checks the valid operations on the corresponding data type.
Framework Class Library
The .Net Framework class library is the collection of reusable types which can be integrated with the Common Language Runtime. The class library is the object oriented. The other main component of the .Net Framework is Framework Class Library which is a standard library. It is the collection of thousands of reusable classes, interfaces and value types.

Sunday, April 17, 2016

Common Language Runtime (CLR)

The Common Language Runtime is the first main component of .Net Framework. The Common Language Runtime main features are to manage memory, thread execution, code execution, code safety verification and other services. CLR is an execution environment for the program code which is defined by CLI. Basically, CLR lies between operating systems and applications which are written in .Net languages. Some of the features of CLR are:
  • Automatic Memory Management – The CLR provides the garbage collection where the lifetime of the objects is managed by garbage collection only and it is known as managed data.
  • Platform Independence – We can build program in any language which targets the CLR, while compiling the program the compiler translates the code into an intermediate language which is CPU independent. It can be executed from any platform which supports the .Net CLR.
  • Security management – It can be achieved through code access Security model. In this, model CLR implements restrictions on managed code with the use of objects called permissions. It specifies what the code can access instead of specifying who can access the resources.
  • Language interoperability – The ability of an application to communicate with another application written in a different programming language. It helps in the reusability of code.

Thursday, April 14, 2016

Common Language Specification (CLS)

The Common Language Specification (CLS) is the set of some basic language features where .Net languages can develop the applications and services, which are compatible with the .Net Framework. If the situation arises of communicating objects which are written in different .Net Complaint languages, then those objects must expose the features that are common to all the languages.
Common Language Specification (CLS) provides the feature of complete interoperability among applications, which is regardless of the language used to create the application.
Common Language Specification (CLS) defines the subset of Common Type System (CTS) and the Common Type System (CTS) describes the set of types that can use different .Net languages are common, it ensure that objects written in different languages can communicate with each other.

Wednesday, April 13, 2016

Common Type System (CTS)

It describes the set of types which can be used in different .Net languages as common. It ensures that the objects which are written in different .Net languages can communicate with each other. But for the communication between programs that are written in any .NET complaint language, the types would have to be compatible on the basic level.
It supports the object oriented concepts that is why all the types are represented in this as objects and also provides support of sharing of common data types. CTS support two different types: Value and Reference type.
  • Value type: The primitive or built in data types of the programming language are known as Value type. It also includes user defined types and enumerations. They are used to store the value of the variable which includes String, Character, Integer, etc. These data types are passed in the method by using by value method. 
  • Reference Types: In the reference types they store a reference to the value's memory address, and are allocated on the heap. They can be self-describing types, pointer types, or interface types. Reference types are those whose data type objects are represented by a reference to the object's actual value.

Tuesday, April 12, 2016

Common Language Infrastructure

The Common Language Infrastructure (CLI) is developed by Microsoft and is standardized by ISO and ECMA which helps in running high level language application programs in different computer systems. The CLI allows multiple high-level languages to be used on different platforms without being writing again for the application. The CLI has four aspects which are as follows:-
  • CTS (Common Type System) - The set of data types and operations which are shared by all the CTS-compliant programming languages.
  • Meta data - It describes all those classes and class members which are defined in the assembly, and those classes and class members which are in the current assembly and will be called from the another assembly. 
  • CLS (Common Language specification) - The set of basic rules for any language which is targeting the CLI should conform to interoperate with other CLS-compliant languages. 
  • VES (Virtual Execution System) - The VES will load and execute CLI-compatible programs using the metadata to combine with the separated executed pieces of code at runtime.

Monday, April 11, 2016

Features of .Net Framework

  • Memory Management – In many other programming languages the responsibility of developers is to allocate and reallocate the memory but in .Net Framework CLR is responsible for all these.
  • Common Type System – In other programming languages, the basic types are defined by the compiler only, where interoperability is not possible whereas in the .NET Framework, the basic types are defined by the .NET Framework type system which are common to all languages which are the part of the .NET Framework.
  • Extensive class library – The programmers can use the readily accessible library of various data types and members from the .NET Framework Class Library instead of writing the vast amount of codes.
  • Version compatibility – Applications which have been developed by using a particular version of the .NET Framework can run without modification on a later version.

Sunday, April 10, 2016

Introduction of .NET Framework

The .NET Framework is a development platform for application which provides services like building, deploying, and running applications of desktop, web, and phone and web services. It can be called as Development platform or Execution environment which consists of tools and technologies, to develop distributed applications and distributed web services.
The .Net Framework mainly consists of two major components that are common language runtime (CLR), which provides memory management and other services, and an extensive class library, which includes tested, reusable code for all major areas of application development.

Thursday, April 7, 2016

C# Overview & Evolution of C# Language

C# language was introduced by Microsoft in the middle of 2000 and it was developed by Anders Hejlsberg. It is an elegant and type-safe object oriented language which enables the developers to build the secure and robust applications which runs on .NET Framework.
C# is used to create traditional windows client applications, XML web services, client-server applications and many more. It is easy and simple to learn. The coding of c# language is same as to java. As it is objected oriented language it supports some of the features like encapsulation, inheritance and polymorphism.
Some features of C#:
  • Garbage Collection: It automatically reallocates the memory which is occupied by the unused objects.
  • Exception Handling: It provides a structured and extensible approach to detection and recovery.
  • It has a unified type system where all C# types includes primitive types like int and double which are inherit from single root object type.
C# Versions
  • C# 1.0 – It was the first release and was included in Visual Studio.Net.
  • C# 1.1 – It was released after the first release and Microsoft changed the Visual Studio to Visual Studio.Net 2003 in year 2003.
  • C# 2.0 – It was released in year 2005 as Visual Studio 2005. 
  • C# 3.0 – It was released with the same Visual Studio with the previous one and it was integrated with Windows Vista and Server 2008.
  • C# 3.5 – AJAX – Visual Studio 2008
  • C# 4.0 – It was released with the Visual Studio 2010.
  • C# 4.5 – It was released with the Visual Studio 2012.
  • C# 4.5.1, 4.5.2 – It is released with Visual Studio 2013.

Wednesday, April 6, 2016

'Extern' Storage Class Specifier

You can use extern keyword along with function declaration as well, but that's a redundant operation since functions have external linkage by default. ie; They can be accessed globally from other places.

'Typedef' Qualifier

'typedef' is not a storage class specifier, but it allows us to give an alias (new name) to existing data types (built-in or user defined datatypes). Sytax of 'typedef' is as given below,
typedef data_type alias; 
Let's consider the following example
typedef int number;
Here, we've given an alias to data type 'int' which is 'number'. So now we can use 'number' as a type in variable declaration and definition to hold an integer values. By creating an alias, we can use it as given below
number first, second;
It means 'first' and 'second' are of the 'number' data type which in turn is an integer. So, 'first' and 'second' are also integers.

Summary of Storage Class Specifiers

Table given below summarizes the storage location, initial default value, scope and life time of different storage class specifiers
Storage ClassSpace Allocated inDefault Initial ValueScopeLife
autoMemoryGarbageLocal ScopeAlive within the declared code block
registerCPU RegisterGarbageLocal ScopeAlive within the declared code block
staticMemory0Local ScopeValue retained between invocations
externMemory0Global ScopeAlive till program exit

Tuesday, April 5, 2016

'Static' Storage Class Specifier

  • Value of a 'Static' variable will be maintained across the function invocations. If a variable is declared using 'Static' qualifier at the top of the source file, then the scope of that variable is within the entire file. Files outside the declared file can't access the declared static variable. 
  • 'Static' variables declared within a function or a block, also known as local static variables, have the scope that, they are visible only within the block or function like local variables. The values assigned by the functions into static local variables during the first call of the function will persist/available until the function is invoked again.
  • 'Static' variables are by default initialized with value 0.

Let's see the effect of declaring a local and global variable with 'Static' keyword.
  1. #include <stdio.h>
  2. static int global_static_variable = 1;
  3.  
  4. static void static_variable_test() {
  5. static int local_static_variable;
  6. printf("local_static_variable:%d\t", local_static_variable);
  7. local_static_variable++;
  8. printf("global_static_variable:%d\n", global_static_variable);
  9. global_static_variable++;
  10. }
  11. int main()
  12. {
  13. static_variable_test();
  14. static_variable_test();
  15. }
  16.  
  17. Output:
  18. local_static_variable:0 global_static_variable:1
  19. local_static_variable:1 global_static_variable:2
Few points to note here are
  1. 'Static' variable got initialized with value 0 by default. (We didn't initialize the variable 'local_static_variable' with a value, but it got initialized with 0 automatically)
  2. Value of the the local variable 'local_static_variable' is retained across invocations of function 'static_variable_test()'
'Static' and Global functions/variables
In C programming language, functions are global by default. That means, by default function declared in a file can be accessed from code with in another file. But when you apply static keyword to a functions, it makes the function accessible only from that file. So if you want to restrict access to a function from code in other files, you can declare the function as static.

In the example given above, the function 'static_variable_test' will be visible only with in the file it's declared. Also, if you declare a global variable as static, code in other files cannot access the variable. In simple words, declaring a global function or variable as static gives it internal linkage.

Monday, April 4, 2016

'Register' Storage Class Specifier

'register' storage class is same as 'auto' except that 'register' variables are stored in CPU registers compared to 'auto' where 'auto' variables are stored in RAM. The scope of 'register' class variable is same as 'auto' class variable, i.e. they can't be accessed outside the code block they are declared. 'register' variables will also be initialized with garbage value.

Example of register storage class is given below
  1. #include <stdio.h>
  2. int main( )
  3. { //Code block 1
  4.  
  5. auto int a = 1;
  6. { //Code block 2
  7.  
  8. auto int a = 2;
  9. { //Code Block 3
  10.  
  11. auto int a = 3;
  12. printf ( "\n Value of 'a' in Code Block 3: %d", a);
  13. }
  14. printf ( "\n Value of 'a' in Code Block 2: %d", a);
  15. }
  16. printf ( "\n Value of 'a' in Code Block 1: %d", a);
  17. }
  18.  
  19. Output:
  20. Value of 'a' in Code Block 3: 3
  21. Value of 'a' in Code Block 2: 2
  22. Value of 'a' in Code Block 1: 1
Since CPU register access is faster than memory access, declaring frequently accessed variables as 'register' will improve the performance of program. But one important thing to note about 'register' variable is, it's not guaranteed that those variables will be stored in CPU registers because number of registers is very limited for a CPU. So, if all the registers are allocated for other variables or purposes, then a variable will get stored in RAM even if it's declared as 'register' type.


Also, you cannot request address of a register variable using address operator (&). This will result in compilation error because 'register' variables might not be stored on RAM.

Sunday, April 3, 2016

C Storage Class Specifiers - auto, register, static and extern

We know that, a specific number of bytes of space is required to store a value of a variable as needed by the type of variable. For example, 'char' requires 1 byte, 'int' requires 2/4 byte and so on. This space can be allocated from the memory of the system or from processor registers. Register access is always faster than memory access. What if we can specify to the compiler about where to store the variable? Also, how long to keep the value of variable in memory, it's initial value etc? Storage class specifiers are used for that purpose. Using storage class specifiers we can specify
  1. Where to store the value of variable: registers of memory
  2. Specify the scope of the variable
  3. Specify life time of variable
Storage class specifiers available in C programming language include 'auto', 'register', 'static' and 'extern'.

'Auto' Storage Class Specifier

A variable declared as 'auto' qualifier implies that the variable exists/accessible only with in the code block they are declared. For all the code blocks outside the declared code block, it's as if the variable doesn't exist. If some code outside of the code block tries to access an 'auto' variable, it will cause compilation error. By default, all variables are of auto storage class.

Let's take a look at an example of 'auto' storage class.
  1. #include <stdio.h>
  2. int main( )
  3. { //Code block 1
  4.  
  5. auto int a = 1;
  6. { //Code block 2
  7.  
  8. auto int a = 2;
  9. { //Code Block 3
  10.  
  11. auto int a = 3;
  12. printf ( "\n Value of 'a' in Code Block 3: %d", a);
  13. }
  14. printf ( "\n Value of 'a' in Code Block 2: %d", a);
  15. }
  16. printf ( "\n Value of 'a' in Code Block 1: %d", a);
  17. }
  18.  
  19. Output:
  20. Value of 'a' in Code Block 3: 3
  21. Value of 'a' in Code Block 2: 2
  22. Value of 'a' in Code Block 1: 1
From the above example, you can see that value of variable 'a' is different in each code block because they are declared as 'auto' and has scope only with in the declared block. The behaviour would be same even if you remove the 'auto' keyword from the variable declaration, because by default all variables are of type 'auto'.

Life of 'auto' variable is within the declared code block, they will cease to exist after the execution of corresponding code block and the initial value of variable would be garbage. So they need to be initialized with proper value before usage.