C# -Type Conversion

Type Conversion means to Convert one data type to another. Generally type conversion is done in two form
  1. Implicit Type Conversion : In This type of conversion the conversion is done automatically by the compiler, from smaller data type to larger data type or from derived classes to base class
  2. Explicit Type Conversion: In This type of conversion the conversion is done automatically by the compiler, from smaller data type to larger data type or from derived classes to base class
Implicit Type Conversion Example:

1
2
3
4
int i_num;
 i_num = 78;
 double d_num;
 d_num = i_num; // Implicit Conversion

Explicit Type Conversion Example :

1
2
3
4
int i_num;
double d_num;
d_num = 78.45;
i_num = (int)d_num; //Explicity Type Conversion

C# provide three difference ways for data type conversion
  1. Type Cast Operator () 
  2. Convert Class
  3. Paring
Type Cast operation we have used in the above example. The second is Convert Class  which has different methods.

Method Description
ToBoolean Converts a type to a Boolean value, where possible.
ToByte Converts a type to a byte.
ToChar Converts a type to a single Unicode character, where possible.
ToDecimal Converts a floating point or integer type to a decimal type.
ToDouble Converts a type to a double type.
ToInt32Converts a type to a 32-bit integer.
ToInt16Converts a type to a 16-bit integer.
ToInt64Converts a type to a 64-bit integer.
ToStringConverts a type to a string.

The following example converts various value types to string type −

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;

namespace TypeConversionApplication {
   class StringConversion {
      static void Main(string[] args) {
         int i = 75;
         float f = 53.005f;
         double d = 2345.7652;
         bool b = true;

         Console.WriteLine(i.ToString());
         Console.WriteLine(f.ToString());
         Console.WriteLine(d.ToString());
         Console.WriteLine(b.ToString());
         Console.ReadKey();
            
      }
   }
}

When the above code is compiled and executed, it produces the following result
75
53.005
2345.7652
True

Variables and Data types - C# Basic Programming

Objective

  • Different types of C# data types, their sizes and storing capacities
  • Difference between value types data types and reference type data types
Variables and data types are basic requirements of any programming language. C#.Net is a strongly typed language, it means each object and variable must be declared with a type. There are various types of data types in C# that can be used to define variable. A data type is categorized with value type and reference type. int and char is the value type data type where object is the reference type data type. You can also create user defined data type using Classes and Object. We will study this later.

C# is a strongly typed language. It means, that you cannot use variable without data types. Data types tell the compiler that which type of data is used for processing. Such as if you want to work with string value then you will have to assign string type variable to work with. C# provides two types of data types: Value types and Reference types.
A Value type data type stores copy of the value whereas the Reference type data types stores the address of the value. C sharp provides great range of predefined data types but it also gives the way to create user defined data types.
A complete detail of C# data types are mentioned below:

Value Types:

TypeRepresentsRangeDefault Value
boolBoolean valueTrue or FalseFalse
byte8-bit unsigned integer0 to 2550
char16-bit Unicode characterU +0000 to U +ffff'\0'
decimal128-bit precise decimal values with 28-29 significant digits(-7.9 x 1028 to 7.9 x 1028) / 100 to 28 0.0M
double64-bit double-precision floating point type(+/-)5.0 x 10-324 to (+/-)1.7 x 103080.0D
float32-bit single-precision floating point type-3.4 x 1038 to + 3.4 x 10380.0F
int32-bit signed integer type-2,147,483,648 to 2,147,483,6470
long64-bit signed integer type-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0L
sbyte8-bit signed integer type-128 to 1270
short16-bit signed integer type-32,768 to 32,7670
uint32-bit unsigned integer type0 to 4,294,967,2950
ulong64-bit unsigned integer type0 to 18,446,744,073,709,551,6150
ushort16-bit unsigned integer type0 to 65,5350

Reference Types:

Data TypesSizeValues
stringVariable length0-2 billion Unicode characters
object------
A variable refers to the memory address. When you create variable, it creates holds space in the memory that is used for storing temporary data. As you know about c# data types, each data type has predefined size. In this chapter you will learn how to use data types to create variable.

























Output :
Please Enter First Value :
10
Please Enter 2nd Value :

20
Sum of 10 and 20 is 30


In the preceding example we create three integer type variable num1,num2 and result. num1 and num2 is used for accepting user value and result is used for adding both number.

for input we used Console.ReadLine(); method but the values should be converted to desire type so we used int.Parse() method because ReadLine() takes input as String.


num1 = Int32.Parse(Console.ReadLine());

You can use the following method to convert one data type to another data type.


Integer = int32.parse() or Convert.ToInt32();
 Float= (float)
 Double=Convert.ToDouble();
 Decimal=Convert.ToDecimal();
 Byte=Convert.ToByte();

Write a program in which accepts user’s details as name, city, age and pin number. Then store all the values in the appropriate variable and then print all the information in correctformat

EXAMPLE:













































Output:

Enter Your Name : SUNNY

Enter Your City : PESHAWAR

Enter Your Age : 30 

Enter Your Pin : 954878 

========================
Your Complete Address
========================

Name : SUNNY
City :  PESHAWAR
Age : 30
Pin :954878

How to debug or execute C# program in Visual Studio?

Using Visual Studio 2005/2008/2012/2013 or any version

Visual Studio is easiest way to handle C# code. To execute the program on visual studio, go through the following steps:
Step 1: Launch Visual Studio and go to File > New Project

Step 2: Select Visual C# in left pane and then choose Console Application. Name your Project and select Location and ok


Step 3: Visual Studio will open a code editor window including some necessary code.

Step 5: Now write now Write in Main method the following line of code  code and press F5 to run C# code.
Console.WriteLine("Hello Welcome to ChashLearner");
Step 6: You will see the  output in Visual Studio Command Prompt.

How to debug or execute C# program in Notepad?


Objective
  1. How to compile and run c# code in notepad as well as visual studio?
  2. How to run C# code in notepad as well as visual studio?

It has been seemed with various beginners that they are able to understand C# concept but they got failed while implementing their logic into program. The best way to get rid of this types of problem is practice. So, just understand the program that is mentioned below and try to run C# code on your system.

Using Notepad

Step 1: Open Notepad and write the following c# code carefully


Step 2: Now save this program as firstprogram.cs in csharp Folder in D drive (Create csharp Folder in D Drive). You must choose All Files in save as type box.

















Step 3: Go to Start > All Programs > Microsoft Visual Studio 2005 or 2008 > Visual Studio Tools > Visual Studio 2005 or 2008 Command Prompt.


  • Type D: and hit Enter
  • Now type cd csharp and hit Enter
  • You are in Csharp Folder now

 
Step 5: Now compile your program by following command:











You will see some information like compiler version number and framework information. There is no error, so you won’t get any error.


Step 6: Now, it’s turn to C sharp program. Just write your file name without extension and hit enter.














Step 7: 
You will get the This is my First C# Program  as output.

Installing Microsoft Visual Studio


Visual Studio supports different programming languages and allows the code editor and debugger to support (to varying degrees) nearly any programming language, provided a language-specific service exists. Built-in languages include C, C++ and C++/CLI (via Visual C++), VB.NET (via Visual Basic .NET), C# (via Visual C#), and F# (as of Visual Studio 2010). Support for other languages such as M, Python, and Ruby among others is available via language services installed separately. It also supports XML/XSLT, HTML/XHTML, JavaScript and CSS. Java (and J#) were supported in the past.

Part 1 of 3: Downloading Visual Studio

1 Download Visual Studio Express 2013. You can download the software from the Visual Studio website.



2 Sign in to your Hotmail / Live account. You will be required to sign in to your Hotmail or Live account, if you have one. If you don't have an account, you can create one.


 


3 Click on your download. The specific software version you have requested will be listed under "Your Selection". Click on it to begin the download.

Part 2 of 3: Installing Visual Studio

1 Click "Install". Be sure to read and agree to the License Terms and Privacy Policy.

 

2 Wait for the install to complete. Before installing, a System Restore Point will be automatically created.


3 Click Launch once the install has completed. Allow a few minutes for Visual Studio to finish configuring its first startup.