Data is represented in any programming language by its Data
types.
It specifies 3 things:
- What value can the data type take.
- How many bytes would the data type occupy in memory.
- What operations can be performed on it.
Example:- integer data type value range is -2147483648 to
+2147483647,it occupies 4 bytes in memory and operations like addition,
subtraction, multiplication, division, etc. can be performed on it. Similarly,
a bool data type can take a value true or false, occupies 1 byte in memory and
permits comparison operations on it.
Based on where a data type can be created in memory, it is
called a value type(in stack) or reference type(in heap).C# forces certain data
types to get created only in stack(ex: integer) and certain only in heap(ex:
string).This can’t be changed by us. All data types that are small in size are
created in stack and the other in heap.
A value type as well as reference type can be further
categorized into pre-defined/primitives(ex: integer) and user-defined(ex:
structure) categories.
To use these data types, we need t create constants and
variables. A constant is a specific value from the range(Ex: for char it is 0
to 65535) of values offered by the data
type, whereas a variable is a
container(a memory location) which can hold a constant value. A
constant/literal value can’t be changed but a variable/identifier can.
There are certain rules for creating constants and
variables.
For Constants:
- If no sign precedes a numeric constant, then assumed as +ve.
- No commas or blanks allowed.
- Bytes occupied by constant are fixed and can’t change from one compiler to another.
- Only float constants(must be followed by suffix f) can take decimal point.
- Float constants can be expressed in fractional(ex: 314.58f) or exponential(ex:3.1458e2f)
- A character constant(max. length 1 character) is a single alphabet/digit/special symbol enclosed within inverted commas pointing to left. ’A’ is valid but not ‘A’.
For Variables:
- Variable name is combination of alphabets,digits,underscores with maximum length 247 charactes by some compilers but recommended 31.First character should be alphabet.
- No commas or blanks allowed and no special symbol other than underscore.
- Variable names are case sensitive.
We need to declare a variable type before using it.
Ex: int si,hra;
Ex: int si,hra;
C# Keywords: whose meaning is already defined.int is a keyword above. If we want to
use the keyword name for variable use @ symbol. Ex: @if
List of keywords mentioned in below table.
In addition to above keywords, which have special meaning in
a limited program context are known as Contextual Keywords. As C# evolves new
keywords are added to it as contextual to avoid breaking programs written in
earlier versions of C# language.
First C#
program
Before we begin, let’s see the below common programming
rules.
- Each instruction is a separate statement.Therefore,a complete C# program consists a series of statements.
- The statements should appear in same order which we wish them to be executed; unless of course the logic of problem demands a deliberate ‘jump’ or transfer of control to a statement, which is out of sequence.
- Blank spaces may be inserted between 2 words but not allowed within a variable,constant or keyword.
- All statements are in small case letters.
- Every statement must end with a semicolon (;).Thus ; acts as a statement terminator.
/*Calculation of simple interest*/
using System;
namespace SimpleInterestProject
{
class SimpleInterest
{
static void Main(string args[])
{
float p,r,si;
int n;
p=1000.50f;n=3;r=15.5f;
si=p*n*r/100;
Console.WriteLine(si); // For printing data to console.
}
}
}
Some
details about above program:
- Comments
à Lines
skipped by compiler in the program.
// à Singleline comments. /* */ à Multiline comments. - Every
instruction used in program should belong to a function.Every function must belong to a class and every
class should belong to a namespace.And main function
is the start point of execution.
- print data to a separate line, to print in same line use Write() method.If we want to get input from keyboard using Read()
and ReadLine().But it will take all the values as
strings.If any numeric value exists, we need to
convert them using ToSingle() à
float,ToInt32()à32bit
integer functions.
- To
supply any command line arguments use solution explorer properties node debug
option command line argument box in visual studio.We
can parse those values using args[] variable in our
Main program,like args[0],args[1] depending on
number of arguments.
Steps
for Compilation and Execution
- Start
Visual Studio 2008 from Start|All Programs|Microsoft Visual Studio 2008
- Select
File|New Project…from the File menu.Select project type as Visual C#|Win w32 Console App
from the dialog pops up.Give your name and click
finish.
- Visual
studio would provide just a skeleton program with empty main program
created.
- You can
delete the unnecessary namespaces and we can delete string args[]
from Main() if no arguments are there to execute in the
program.
- Save
your program after typing your statements by using Ctrl+S.Compile and execute using Ctrl+F5.
C#
provides many flavors of integers and reals.
Integer
Types:
Default
type is int.
Real
Types:
Default
type is double.If we wish to treat it as float or
decimal,we need o suffix f or
F and a m or M for decimal.
float x=3.5; //error
float x=3.5f; //correct
doublet x=3.5f; //correct
double x=3.5; //correct
decimal x=3.5; //error
decimal x=3.5m; //correct
If
you want an integer number treated as double,use the
suffix d or D.
double x=3d;
double x=3d;
If the
value of float/double/decimal is too small or large then use exponential
form.
float a=3.41295e-5f; //0.0000341295f
float a=3.41295e-5f; //0.0000341295f
When a
real number is stored in a float/double/decimal it is stored in binary form.
During this conversion some precision may be lost.
char data
type represents a character in Unicode format.
In addition to the normal form a character constant can also be specified in hexadecimal escape sequence and Unicode representation.
char ch=’X’; //character literal
char dh=’\x0058’; //hexadecimal
char eh=’u0058’//Unicode
bool Data
Typeàvalues
true/false.Like c or c++
here bool is not integer but a separate data
type.




No comments:
Post a Comment