Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
STETPS OF EXECUTION
Save file with .cs extention Setting the path of C# compiler Right click on mycomputer Advanced system settings advanced environment variable path variable and set path as: C:\WINDOWS\Microsoft.NET\Framework\v3. 5 />csc filename.cs //Compilation />classname //Running the program
DATA TYPES IN C#
The keywords used to define the kind of data and amount of data inside a variable or constant are called data types.
Value Types
Used
to hold some data and allows to perform some action on that data
They are again of four types
INTEGRALS
byte 1 byte (unsigned byte) short 2 byte (signed) int 4 byte (signed) long 8 bytes (signed) sbyte 1 byte (singed byte) ushort 2 byte (unsigned) uint 4 byte (unsigned) ulong 8 byte (unsigned)
Floatings
float 4 bytes 7 decimal accuracy double 8 bytes 15 decimals decimal 16 bytes 28 decimals
Characters
Booleans
REFERENCE TYPES
To
string
object
These data types can be created as per needs and are of the following types:
Class
LITERALS
The value that we assign from our side at the time of declaration in the form of assignment or expressions is called as literal or literal value. Can be of different types
Integrals Literals
Default
is int
int num=67;
Use
LITERALS CONTD..
Floating
Default
Literals
is double
double a=5.6;
Use
float x=5.6; //compile time error float x=5.6F; // no error decimal p=5.6M;
LITERALS CONTD..
Character Literals
String literals
Boolean literals
Can be true or false Default is false
bool married=true;
WRITING OUTPUT
Using Place holders in parameter of WriteLine() Use parameters as {0}, {1} etc. independent of data types
int a=5,b=6; Console.WriteLine("Sum of "+ a +" and "+ b +" is "+ (a+b)); //java Style Console.WriteLine("Sum of {0} and {1} is {2}" ,a,b,a+b); // Using placeholders
More Examples on placeholders Console.WriteLine(Emp id = {0} experience = {1} and age= {2}, id, exp, age); Console.WriteLine(Emp id = {0} experience = {1} and age= {2} , test = {0} , id, exp, age);
To read the data from keyboard use ReadLine() method of Console class
To convert a string into a number, use Parse() method of the data type.
Example Write a program to get name and age of a person and check it to be valid voter Name string Age int
CONTD..
using System; class Program { static void Main(string[] args) { Console.Write("Name : "); string name = Console.ReadLine(); Console.Write("Age : "); int age = int.Parse(Console.ReadLine()); if (age >= 18) Console.WriteLine("Dear {0} you can vote", name); else Console.WriteLine("Dear {0} you cannot vote", name); }
THANKYOU