Before we begin with C sharp, C# programming, let’s talk about what is C# and .NET Framework. C# is an object oriented programming language that enables developers to make various type of desktop, web or mobile applications using .NET Framework library.
.NET Framework is a software framework developed by a Microsoft that runs primarily on Microsoft Windows. It includes a large class library known as Framework Class Library and provides a language interoperability. Programs written for .NET Framework execute in a environment known as a Common Language Runtime (CLR), an application virtual machine that provides services such as security, memory management, and exception handling.
Here’s the basic structure of console application in C# :
using system; //namespace declaration class test { public static void main() { Console.WriteLine("This is a sample program"); } }
In the above application, the first line “using system” indicates that program is using System namespace. So, what is namespace? Namespace is a collection of classes, structs, enums, interfaces and delegates. Namespace are usually used to organize the project and distinguish classes with same name.
Create a C# Console Application
Let’s create a console application. Open a visual studio 2013 and click on File at the top. Select a new project and select console application. Give a name to console application and click Ok button.
After clicking ok, the following files are automatically created by visual studio.
Reading and Writing to C# Console Application
Let’s start with reading and writing to console.
We can read an user input in console by using these static methods: Console.Read(), Console.ReadLine() and Console.ReadKey(). Each of them has their own purpose:
1. Console.Read() : this method accept character and returns each value as an integer until zero is reached which signifies the end of user’s input 2. Console.ReadLine() : this method accept string and returns string after Enter key is pressed 3. Console.ReadKey() : this method accept character and returns ASCII value of character
There are two ways to writing in a console. They are: 1. Placeholder syntax 2. Concatenation
Placeholder syntax is the preferred way to write in the console. We using following static methods to write in the console.
1. Console.Write() : these method renders text in the same line on the console window 2. Console.WriteLine() : these method renders the text in new line
Here’s a sample program which demonstrates read and write to the console.
using System; class test { static void main() { Console.WriteLine(“Enter your name:”); string username = Console.ReadLine(); //reads the name entered by user Console.WriteLine(“Username is ”+username); //concatenation Console.WriteLine(“Username is {0}. Hello {0}”, username);//Placeholder syntax } }
Run the console application by pressing Ctrl + F5 key. In the console window, the text “Enter your name: ” is rendered . After you type your name in console window and pressed Enter key, you will get following output.
Output: Username is Sachit Username is Sachit. Hello Sachit
In next part, we will discuss about C# Built In Type and Common Operators.