Learn C# Basics | Every Thing You Need to Start C# Programming |
What is C#?
C# programming language was designed and developed by the Microsoft in the year 2002. It is based on .NET framework. This framework let the developers to build secure and dynamically powerful applications. It is an object-oriented language which provides a huge bunch of interfaces, libraries, and properties.It is widely used in developing application such as Mobile application and Web applications. Programmers also use it to build Desktop and database applications. Its framework has tons of properties and class in the class library. .NET comprises of CLR (Common Language Runtime) which is responsible of allocating most crucial services to the framework.
C# Variables:
Variables are responsible for storing values. C# variables are declared after a data type is specified. C# Variables can store any value of numbers or strings. C# basics may include the declaration of these variables. C# variables can be composed of letters, number, and the underscore character. However, a C# variable must start with an underscore or a letter. You should name your variables properly so that your code remain understandable and clear. C# variables can be declared as follows:
- string name;
- int price = 10;
Also, you can write it as:
- int price;
- price = 10;
C# Data Types:
Declaring Data type, before a variable is necessary. C# data types includes the following built-in data types:
string: a set of characters/alphabets.
int: all numerical values.
float: numbers with Floating/Decimal points.
double: a higher version of float.
char: a single character.
bool: only two values i.e, True or False.
main()
{
int a = 5; double b = 5.86555584; bool areWe = false; string _youi = "HowTechyy";
}
Your First C# Application:
We will need an IDE (Integrated Development Environment) in order to code. Microsoft Visual Studio provides number of useful tools to develop and code. Visual Studio Communty version is a free available version of Visual Studio, as it will be more than enough for C# basics.
Install and open Visual Studio with default configuration. Choose File > New > Project and select console application. You will a few code statements generated by visual studio, don't worry as we will discuss about them in a later article. For now in C# basics, just keep one thing in mind that every program has a staring point which known as Main method.
Finally, hit ctrl+F5 and a window(output) will pop up showing the result of your code. It might be empty as we have not done any coding yet. Therefore, Congratulations you just have created your first C# application!
C# Prints and Output:
Every programming language takes input from users and print the results. For the output result of the content, we use Console.Write or Console.WriteLine command. They have a little bit of difference of printing on the same line or skipping that line.
- main()
- { Console.WriteLine("Learn C# Basics"); }
- //output: Learn C# Basics
It is also possible to print variables in C# prints and output as shown below:
- Main()
- {
- int a = 12; Console.WriteLine(a); }
- //output: 12
However you can also insert variables as per c# specific format to use and print the values dynamically:
- Main()
- {
- int a = 12; Console.WriteLine("I am using Howtechyy for {0} Years",a); }
- //output: I am using Howtechy for 12 years
C# User Input:
Command used to take inputs from the users in C# is Console.ReadLine(). It is one of the most used thing in the C# basics. Following program illustrates how C# takes user input and prints the commanded output accordingly.
main()
{
string name;
Console.WriteLine("What is your Blog?");
name = Console.ReadLine();
}
/*Output: What is your BLog?
....
HowTechyy
C# user inputs are basically strings. If you want to take input as an integer value then you will need to convert it. Dot Net framework provides conversion extensions such as:
- Convert.ToDouuble
- Convert.ToBool
- Convert.ToInt16
- Convert.ToInt32
Let's see how can we use it in our program:
Main()
{
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("This is {0}", year);
}
- //output: ...2019
- This is 2019
C# Comments:
C# basics may include a very useful thing which is C# comments. These are those explanatory statements that are ignored by the complier and does not executed. There are two types of C# comments. Single-Line Comments and Multi- line Comments.
Single Line comments use double forward slashed to ignore a single line by the compiler as shown:
- main()
- { Console.WriteLine("Learn C# C# comments"); }
- //output: Learn C# Basics (Compiler will ignore this statement)
Multi-line comments use a forward slashed with an asterisk (*) to ignore a single line by the compiler as shown:
- main()
- { Console.WriteLine("Learn C# comments"); }
- /*output:
- Learn C# Basics *\ (Compiler will ignore these statements)
C# Arithmetic Operators:
C# arithmetic operators comprises of simple addition, subtraction, multiplication, division, and modulus. It is very important cover arithmetic and logical operators in C# basics.
C# arithmetic operators are:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- main()
- {
- int x = 5;
- int y = 10; Console.WriteLine(x + y);
- Console.WriteLine(y - x);
- Console.WriteLine(x * y);
- }
- /*output: 15
- 5
- 50 *\
C# Assignment Operators:
C# assignment operators comprises of equal to(=) sign. However, the definition of C# incremental operators is very important in c# basics to keep in mind. Incremental operators increments the value of a variable by one unit. Incremental operators are used as follows:
Main()
{
x += y; //its means x = x + y;
- x -= y; //its means x = x * y;
}
- //output: ...2019
- This is 2019
0 Comments
Your comments and interesting view make our day!