As we know that now a days C# plays a major role in most of the Web technologies to write the server side code, Ex: ASP.NET, ASP.NET MVC etc., Lets start with the Basic programming in C# in this chapter.
Introduction
We have already seen the Introduction and Features of
C# programming language in the previous chapter. Now, we are going to see the basic programming concepts in here.
Getting Started
- At First Open the Visual Studio in your computer.
- Now, choose New Project from the File menu which opens a dialog box for creating New Project like below,
- Select Installed, expand Templates, then expand Visual C# and select Console Application like above.
- Give a name to your Project as above, and click Ok to create a project in Solution Explorer.
- After creating project, it will opens Program.cs by default as below,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hello
{
class Program
{
static void Main(string[] args)
{
}
}
}
- Now, replace the above code with the below one,
using System;
namespace Hello
{
class Program
{
static void Main()
{
Console.WriteLine("Hello ! Welcome to C# Programming");
}
}
}
- Press F5 key to open up a Command Prompt window which shows you the mesage "
Hello ! Welcome to C# Programming
" like below,
Description
Let us re-call the above code once again to learn what's in it.
using System;
namespace Hello
{
class Program
{
static void Main()
{
Console.WriteLine("Hello ! Welcome to C# Programming");
}
}
}
In the above code., there are so many
sections
or
blocks
.
Body:
Below statement is called as the Heart of the above program.
Console.WriteLine("Hello ! Welcome to C# Programming");
In
C#, statements execute sequentially and terminated with a
semicolon(
;
) or a
code block(
}
).
Statement Block:Statement block is a pair of braces containing any number of elements or might be zero elements, which performs an action in a series of statements.
Ex:
static void Main()
{
....
.....
}
Comments or Documentation in C#:C# supports two ways of Code-Documentation or Comments. They are, single line
and multi line
.
Below example is the syntax of using comments in C#.
// This is a single line comment
/* This is a multi line comment
with two lines */
Console:
Console is a static class and also part of .Net Framework. It represents the standard I/O streams and error streams for console application.
Console class cannot be Inherited.
Full name is System.Console.
Conclusion
In this article we have seen creating a simple program in C# with a little description. Hope you understand it well. If you get any doubt on this, please ask in the below comments.
Thanks for reading.
Regards,
Krishna.