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
We can declare and the initialize the variable at the same time
//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
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:
We are now going to create a simple application as your first C# programme. Before building this application you should already install Microsoft .Net Framework and Microsoft Visual Studio 2008 as we discussed earlier.
And the other thing is you should have a basic idea of
MessageBox.Show("Hello World!");
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace HelloWorld { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Hello World!"); } } }
Step 5: Compile the code
To compile the code Go to Build -> Build HelloWorld
Step 6: Running the application
To run/execute the programme press F5.
Microsoft Visual Studio (VS) is an Integrated Development Environment (IDE) from Microsoft Cooperation. It can be used to develop various kinds of applications using Microsoft .Net Framework for the platforms supported by Microsoft. Those platforms are Microsoft Windows, Windows Mobile, and Windows CE. Using VS, we can develop console and graphical user interface (GUI) applications. Console applications are the applications running on windows terminal. And GUI applications are the applications which have rich user interface such as Windows Forms applications, web sites, web applications and web services.
Visual Studio makes the development process easier and faster. It includes a code editor on which developers can write source codes of the applications. It supports intelliSense as well as code refactoring. It has an inbuilt debugger. Using that, developers can debug .Net applications. Visual Studio has other inbuilt tools. It includes a forms designer for building GUI applications, web designer, class designer and database schema designer. So that developers can build rich applications without doing huge amount of coding. We can create plug-ins for the Visual Studio to enhance the functionality of the tool. It supports for source-control systems (like Subversion and Visual SourceSafe) so that many developers can work in a same project.
Here are the most important and useful Visual Studio 2008 features.
• IntelliSense
• Code Snippets
• Revision Marks
• Designer Improvements
• XML Support
• Refactoring
• Partial Classes
• Debugging
• Other IDE Improvements
Because of those features Visual Studio 2008 becomes one of the main Integrated Development Environment for developing .Net applications.
Microsoft .Net framework is one of the world's greatest development framework developed by Microsoft. And this is a software framework for Microsoft Windows operating systems.Using .Net framework, developers can build standalone desktop applications, web applications and mobile applications. It provides lot's of class libraries to work with user interfaces, data access, database connectivity, cryptography, numerical algorithms and network communications.
The following figure demonstrates how the .Net compilation process is done.
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)