Using variables is one of the key concept when programming.
What are variables?
Variables are used to store values,data within our programme. We need to store numbers, characters, strings, dates and many more things withing our programme. To store them, we have to declare variables.
Variable has a datatype,name and the value.
- Datatype - The type of the variable ( number, character, string, date or etc..)
- Name - The identifier of the variable (Note that variable naming follows Camel casing)
- Value - The value assigned to the variable
Syntax to declare variables:
<datatype> <variableName>;
Assign values to variables: (initialization)
<variableName> = <value>;
We can declare and the initialize the variable at the same time
<datatype> <variableName> = <value>;
//declare and initialize when need string name; name = "my name"; //declaration + initialization string name = "my name";
C# supports many inbuilt data types or we can create custom data types as well.
Here are the C# in-built data types.
Data Type | Range |
---|---|
byte | 0 .. 255 |
sbyte | -128 .. 127 |
short | -32,768 .. 32,767 |
ushort | 0 .. 65,535 |
int | -2,147,483,648 .. 2,147,483,647 |
uint | 0 .. 4,294,967,295 |
long | -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807 |
ulong | 0 .. 18,446,744,073,709,551,615 |
float | -3.402823e38 .. 3.402823e38 |
double | -1.79769313486232e308 .. 1.79769313486232e308 |
decimal | -79228162514264337593543950335 .. 79228162514264337593543950335 |
char | A Unicode character. |
string | A string of Unicode characters. |
bool | True or False. |
object | An object. |
Exercise: Write a programme that accepts your name, age and print them on the console.
Code:
using System; class Program { static void Main(string[] args) { //Declare a variable to store name string name; //Declare a variable to store age int age; //Getting name from the user Console.Write("Enter your name? "); name = Console.ReadLine(); //Getting age from the user Console.Write("Enter your age? "); age = Convert.ToInt32(Console.ReadLine()); //Print name and age Console.WriteLine("My name is " + name); Console.WriteLine("I am " + age + " years old."); } }
Save the file as Program.cs any where you want. You can compile c# using the command line version.
csc Program.cs
And then run the new program by entering
Program
Output:
Declaring constants
Constant has a value that does not change. They are declared as a field. C# uses the const keyword before the type of the field.And the most important thing is constants must be initialized as they are declared.
/* declaring a constant * should initialize as declared * value does not change*/ const double PI = 3.14159; /* Error you cannot change the * value of a constant*/ PI = 3.14;
An Example:
using System; class Program { static void Main(string[] args) { const double PI = 3.14159; double radius = 10; double areaOfTheCircle = PI * radius * radius; Console.WriteLine("Area of the circle is " + areaOfTheCircle); } }
Output:
Labels:
c# constant,
c# constants,
c# declare variable,
c# variables,
const c#,
csharp variables,
declare variable in c#
Subscribe to:
Post Comments (Atom)
Labels
- .net framework (5)
- 3.5 net framework (1)
- C# (4)
- c# constant (1)
- c# constants (1)
- c# declare variable (1)
- c# hello world program (1)
- c# variables (1)
- const c# (1)
- csharp tutorial (1)
- csharp variables (1)
- declare variable in c# (1)
- features of visual studio (1)
- hello world c# (1)
- hello world in c# (1)
- install .net framework (1)
- install .net framework 3.5 (1)
- install visual studio 2008 (1)
- net framework install (1)
- programming language (1)
- visual c# hello world (1)
- visual studio (3)
- what is microsoft .net framework (1)
0 comments: