Objective
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.
- 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:
- Different types of C# data types, their sizes and storing capacities
- Difference between value types data types and reference type data types
Value Types:
Type | Represents | Range | Default Value |
---|---|---|---|
bool | Boolean value | True or False | False |
byte | 8-bit unsigned integer | 0 to 255 | 0 |
char | 16-bit Unicode character | U +0000 to U +ffff | '\0' |
decimal | 128-bit precise decimal values with 28-29 significant digits | (-7.9 x 1028 to 7.9 x 1028) / 100 to 28 | 0.0M |
double | 64-bit double-precision floating point type | (+/-)5.0 x 10-324 to (+/-)1.7 x 10308 | 0.0D |
float | 32-bit single-precision floating point type | -3.4 x 1038 to + 3.4 x 1038 | 0.0F |
int | 32-bit signed integer type | -2,147,483,648 to 2,147,483,647 | 0 |
long | 64-bit signed integer type | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L |
sbyte | 8-bit signed integer type | -128 to 127 | 0 |
short | 16-bit signed integer type | -32,768 to 32,767 | 0 |
uint | 32-bit unsigned integer type | 0 to 4,294,967,295 | 0 |
ulong | 64-bit unsigned integer type | 0 to 18,446,744,073,709,551,615 | 0 |
ushort | 16-bit unsigned integer type | 0 to 65,535 | 0 |
Reference Types:
Data Types | Size | Values |
---|---|---|
string | Variable length | 0-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
great
ReplyDelete