Learn C# Basics | Every Thing You Need to Start C# Programming



Learn C# Basics | Every Thing You Need to Start C# Programming
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:

  1. string name;

String is the data type for the variable name, which identifies that the name will contain some string value. We can also assign values to C# variables as shown below:

  1. int price = 10;

Also, you can write it as:

  1. int price;
  2. 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.


  1. main()
  2. {
  3. int a = 5; double b = 5.86555584; bool areWe = false; string _youi = "HowTechyy";
  4. }



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.

  1. main()
  2. { Console.WriteLine("Learn C# Basics"); }

  3. //output: Learn C# Basics


It is also possible to print variables in C# prints and output as shown below:

  1. Main()
  2. {
  3. int a = 12; Console.WriteLine(a); }

  4. //output: 12

However you can also insert variables as per c# specific format to use and print the values dynamically:

  1. Main()
  2. {
  3. int a = 12; Console.WriteLine("I am using Howtechyy for {0} Years",a); }


  4. //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.

  1. main()
  2. {
  3. string name;
  4. Console.WriteLine("What is your Blog?");
  5. name = Console.ReadLine();
  6. Console.WriteLine("Our Blog is {0}", name);
  7. }

  8. /*Output: What is your BLog?
  9. ....
  10. HowTechyy
  11. Our Blog is 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:

  1. Main()
  2. {
  3. int year = Convert.ToInt32(Console.ReadLine());
  4. Console.WriteLine("This is {0}", year);
  5. }

  6. //output: ...2019
  7. 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:

  1. main()
  2. { Console.WriteLine("Learn C# C# comments"); }

  3. //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:

  1. main()
  2. { Console.WriteLine("Learn C# comments"); }

  3. /*output: 
  4. 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 (%)

  1. main()
  2. {
  3. int x = 5;
  4. int y = 10; Console.WriteLine(x + y);
  5. Console.WriteLine(y - x);

  6. Console.WriteLine(x * y);
  1. }

  2. /*output: 15
  3. 5
  4. 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:

  1. Main()
  2. {
  3. x += y; //its means x = x + y;
  4. x *= y; //its means x = x - y;
  5. x -= y; //its means x = x * y;
  6. }

  7. //output: ...2019
  8. This is 2019

Post a Comment

0 Comments