Sei sulla pagina 1di 17

C# DATA TYPES, LITERALS AND USER INPUT

By- Software Incubator

WRITING FIRST C# PROGRAM


//Test.cs class First { public static void Main() { System.Console.WriteLine("Welcome to C#"); } }

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.

They are of two types


Primitive

data types or pre-defined data types User Defined Types

PRIMITIVE DATA TYPES

They again can be of two types


Value

Types Reference 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

char 2 bytes (Unicode) bool 1 bit

Booleans

REFERENCE TYPES
To

hold the address rather than value

string

To hold the address of only strings To hold address of any type

object

USER DEFINED DATA TYPES

These data types can be created as per needs and are of the following types:
Class

(reference type) Structure (value type) Pointers Arrays etc.

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

l or L for long and u or U for unsigned

long k=56L; uint m=67U;

LITERALS CONTD..
Floating
Default

Literals
is double

double a=5.6;

Use

f or F for float and m or M for decimal

float x=5.6; //compile time error float x=5.6F; // no error decimal p=5.6M;

LITERALS CONTD..

Character Literals

Enclosed in single quotes


char ch='A'; char ch=(char)65;

String literals

Enclosed in double quotes

string name="Vikas"; string path=@"c:\keshav"; string name=null;

Use @ to neutralize the escape characters inside a string

Can have the null value

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

WRITING OUTPUT CONTD..


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);

In WriteLine cursor in next line In Write cursor in the same line

DATA INPUT USING KEYBOARD

To read the data from keyboard use ReadLine() method of Console class

public static string ReadLine()

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

DATA INPUT USING KEYBOARD


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

Potrebbero piacerti anche