Constants , Variables And Type Conversions
- Constants - are those variables whose values do not change. Once a value is assigned to constant variable it will be same throughout the program. If tried to assign value to constant Variable in a program after it is already assigned , then it will generate an error.
- Variables- Variables are nothing but a temporary storage area gets created in memory.Variables are used to store values. The value in Variable may change in a program. Each variable has a specific data type which determines how much size it will occupy in memory.
Integer Literal- Contains octal, hexadecimal and decimal constants. There are prefix defined for each type of constants. 0x or 0X is for Hexadecimal constant, 0 for Octal constant and no prefix means Decimal constant.
In other cases, there are also suffix defined with integer constant. There are two types of suffix for integer constants. U for unsigned integer and L for Long integer.
Valid Integer constants-
452- decimal constant
454u- unsigned integer constant
0x3ABu- Hexadecimal Constant
076- octal constant
Floating point Literal - Floating literals is a combination of integer , decimal as well as well as fractional part and exponent part. It also contains suffix as f, F, l L.
Rules
1) You can exclude Decimal part or Fraction part but cannot exclude both while writing Floating point literal.
2) You can exclude Decimal part or Exponent part with signed integer exponent but cannot exclude both.
3) There should be one digit present to the left and right of the Decimal part when writing Floating literal in Fractional Form.
Examples
1.84
3.54e
3.543-4
String literal C# strings are nothing but a set of characters.string literal is a string constant. Basically String literals are divided into two types. Regular String literal and verbatim string literal. Regular strings are similar to other programming language strings like Java ,CPP etc. They are contained in "". Opposite to that , verbatim string literals starts with @ symbol at the beginning of the string.
Example
"Regular"
@"verbatim"
"world"
@"Back"
Character literal- Character literal is a single character enclosed in single quotation mark. For Example 'f'. Character literals also contains escape sequence and universal characters. For example \t , \n , \r.
const DATA_TYPE
VARIABLE_NAME =value
VARIABLE_NAME =value
const int i=1
const float f=3.4
const float f=3.4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Threading.Tasks;
namespace constant_program
{
class Program
{
static void Main(string[] args)
{
const int i = 20;
Console.WriteLine(i);
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Threading.Tasks;
namespace constant_program
{
class Program
{
static void Main(string[] args)
{
const int i = 20;
Console.WriteLine(i);
Console.ReadKey();
}
}
}
There are certain rules to be followed while defining the variable.
1)Variable name must start with Alphabet, letter and underscore.
2)C# defined keywords cannot be used as variable name.
3)Variable names must not contain spaces and special characters.
4)It is good practice not to use all uppercase letters in variables. Upper case letters are primarily used to define constant variables.
DATA_TYPE variable_name
- Type Conversion Type Conversions stands for Converting one data type to another data types. It is also known as Type Casting. Basically having two types.
- Implicit Type Conversion
- Explicit Type Conversion. This type of conversion is done by users on built in data type. Explicit Type Conversion.are done with the help of cast operator.
int i;
float j
string s;
float j
string s;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace var_program
{
class Program
{
static void Main(string[] args)
{
int i;
i=20;
string s;
s="Technology";
Console.WriteLine(i);
Console.WriteLine(s);
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace var_program
{
class Program
{
static void Main(string[] args)
{
int i;
i=20;
string s;
s="Technology";
Console.WriteLine(i);
Console.WriteLine(s);
Console.ReadKey();
}
}
}
This type of conversion is done by the C Sharp itself.being a strongly typed language, this type of conversion is performed in type safe manner by C Sharp.
Output
553. (As it is casted in integer data type, so the remaining part is truncated.)
using System;
namespace var_program
{
class Program
{
static void Main(string[] args)
{
float p=553.4;
int i=(int) p;
Console.WriteLine(i);
Console.ReadKey();
}
}
namespace var_program
{
class Program
{
static void Main(string[] args)
{
float p=553.4;
int i=(int) p;
Console.WriteLine(i);
Console.ReadKey();
}
}
Output
553. (As it is casted in integer data type, so the remaining part is truncated.)
Constants , Variables And Type Conversions
Reviewed by vishal
on
July 28, 2017
Rating:
No comments