Wednesday, October 14, 2020

C# Variables and Their Data Types

Introduction

Variables are temporary data storage memory locations which store program data.

The variable has a name and type. C# is a strongly typed language that means we must declare variable type at the time of declaration.

Rules For Declaring Variable Name

Variable Name must start with an English alphabet letter, _ or @ symbol. The first letter can be either lower case or upper case.

Example of Valid Variable Name declarations: 

fullName;

FullName;

@FullName;

_EmpSalary;

In the middle or at the end of Variable names we can also use English letters that can be either lower or upper case, numbers and underscore ( _ ).

Variable Names are case sensitive, that means someVar and somevar considered as two different variables.

No white space is allowed between variable name, means that full Name is an invalid variable name but we can use underscore ( _ ) character to join words example full_Name is a valid variable name. We can also use camel case to join two words where the first character of second word must be capital for example fullName.

We can use numbers in variable Name but not at the beginning, but we can use numbers in the middle or in the last character example 1stnumber is invalid but number1 is a valid variable name.

We can not use C# keywords as a variable name. It is recommended to choose a variable name in a way that it describes the value that they stored, for example,  courseName is a good choice for storing course name.

Example of Variable Declaration:

syntax of variable declaration

<data_type>  <variable_name>

Where data type must me valid C# data type (we will discuss soon) and variable name must be a valid identifier as we already discussed above.

string courseName;

where string is a data type and courseName is the name of our variable.

where semicolon ; indicates the end of statement in C#.

How we assign value to our  variable.

variable_name = value_to_assign

we always put our variable name on the left side and value to assign the variable on the right side and use operator is also called the assignment operator between them.

courseName = "C# Programming";     <------ The assigned value is in double quotes

The Point to be noted here that we put the assignment value, in the inverted ( " " ) commas, because our variable type is a string and string is nothing but stores the array of characters.

When we execute the following C# code it will print the value of our variable.

Console.WriteLine(courseName);

OUTPUT : C# Programming


Alternative way to declare a variable

string courseName = "C# Programming";

In the above statement, we assigned the value to our variable at the time of declaration.

How To store a single character

For storing a single character we used char data type in C#

char yourGrade = 'A';     <--------- The assigned value is in single quotes

Where char is a data type and yourGrade is a variable name.

We always assign a value to char data type within a single quote as opposite to a  string which requires double quote.

When we execute the following code.

Console.WriteLine("Your Grade is: " + yourGrade);

Then the output will be:

Your Grade is: A 


How to store numbers in C#

For storing whole numbers in C# we used Integer data type.

Example of Integer variable declaration

int number1;

int number2;

Where int is Integer data type and number1 and number2 are our variable's name.

How To Add Two numbers in C#

int sum;    <-------- here we declared a third variable which stores the sum of our two variables.

number1 = 20; <---------- here we assign the value to variable number1

number2 = 4; <--------- here we assign the value to variable number2

The most important point to be noted here that when we assigned the values to our Integer variables we were not using (" "), because it is only used when data type is string.

sum = number1 + number2;

When we run the following command 

Console.WriteLine("Sum of Two numbers is : " + sum);

The Output of the above code will be Sum of Two numbers is :  24

Where "Sum of Two numbers is :" is a string value which we are providing to our Console.WriteLine( ) and we used the + sign and then our variable name sum. Here our variable value is passed as a string to Console.WriteLine( ) and we used + sign because we are concatenating two strings.

We can also use long data type to store whole numbers, but only when need to store very large values.

How to work with Real Numbers in C#

To store real numbers we mostly use three data types

float height;

double pi;

decimal weight;

where floatdouble and decimal are data types and height, pi and weight are variables name.

The actual difference between these three data types is that how big decimal value they can store.

Here we assigned values to our variables storing real numbers

height = 4.9;

pi = 3.14159;

How To Work With Logical Variables in C#:

For logical values we use bool data type in C#. The yes value represents true and no value represents as false in bool data type.

Example of bool 

bool IamADoctor = false;

bool IamAProgrammer = true;

Where bool is a logical data type

When we execute the following code in C# 

Console.WriteLine(“Is I am a Doctor :  + IamADoctor);

Console.WriteLine(“Is I am a Programmer :  + IamAProgrammer);

It will produce the following output.

OutPut 

Is I am a Doctor : false

Is I am a Programmer : true


Declaring Multiple Variables

We can also declare multiple variables in a single line.

For Example:

int a,b,c,d;

Where int is our data type and a,b,c,d are our variable names each separating by comma (,).

Below is a C# Data Type Table given so you have a better idea.


These are some basic Data Types which we had discussed here, there are some other Data Types too. Which we will discuss in our upcoming articles as we need them. 

Here is a YouTube video Link of my C# Variables And Their Data Types tutorial (Urdu/Hindi language)


Written By

Syed Sohail Ahmed Quadri

YouTube Channel

Right Way Learning

Friday, October 9, 2020

C# Programming Introduction

C# is a programming language like Java, C/C++. It is developed by Microsoft and it runs on .NET Framework or .Net Core Framework.

.Net Framework runs only on windows OS but .Net Core is a cross platform framework it can runs on Linux, macOS, windows.

With C# we can develop web applications, desktop applications, mobile applications, games and much more.

C# is an object oriented programming language.

Tool we need to develop C# programming application are visual studio or visual studio code.

In this tutorial series we will use visual studio community edition which is free to download.

C# inherits its basic syntax from C/C++ if you know these two languages you feel home.

Special Notes

C# is a case sensitive language. Incorrect case prevent program from Compiling successfully.

C# application consists of the following parts

  • Declaration of Namespace
  • Class
  • Methods of Class
  • Attributes of Class
  • Main method declaration (starting point of every console application)
  • Statements 
  • Expressions
  • Comments 

Structure of C# Program

Following is a code written in C# programming language

using System;
 namespace HelloWorld
 {
  class Program 
{
  static void Main(string[ ] args) 
{
  Console.WriteLine("Hello World!"); 
                                   /*This is a comment in C# */
}
  }
 }

Explanation Of the above Code


using System;

using keyword is used to import existing classes, so we can use classes in our program define in System namespace.

;

semicolon after the end of every statement is a must in C#,  basically semicolon is a statement terminator.

namespace HelloWorld

namespace is a container for classes and files and "HelloWorld" is the name of namespace in our code.

 {    this curly brace marks the beginning of our namespace.

class Program 

this above instruction declares our class and the name of the class is Program. When we create our console application in .Net framework the " Program.cs " file is  automatically created by the framework which has a class name Program.

 this curly brace right after the declaration of our class marks the beginning of  class code.

static void Main(string[ ] args) 

Every console application has at least one Main( ) method and it is the entry point or starting point of our application, code inside this Main( ) method will be executed.

 this curly brace right after the declaration of our Main( ) method marks the beginning of our Main( ) method.

Console.WriteLine("Hello World!"); 

This Console.WriteLine(  function which is define in System namespace print the " Hello World! " on the Console.

 /*This is a comment in C# */

This above line is a multiple line comment in C# and will be ignored by the C# compiler.


}
  }
 }

These above three curly braces marks the closing of each curly brace which we had open previously in our above code.

When This above code executed it display the following output on the console.

Output

Hello World!


Here is a YouTube video Link of my Introduction To C# Programming tutorial (Urdu/Hindi language)




Written By

Syed Sohail Ahmed Quadri

YouTube Channel   Right Way Learning




Friday, October 2, 2020

Dot Net Core 3.0/3.1 Project Structure As a Back End

Understanding Dot Net Core Files and Folders Structure

When working on Dot Net Core as a Back End the project divides in the following main components and files (We are using Angular as a Front End in this project and Angular project structure already discussed in my previous article).

Connected Services

With Connected Services we can bring Azure workflow into Visual Studio for Mac. For example, you can add an Azure back end services like cloud data storage and authentication.





Dependencies

This folder contains all the internal, external, and third party references required to build and run our project. 








Analyzers helps you maintain high quality code, it can raise  errors/warnings/suggestions when you write less-than-ideal or plain low quality code. 

Frameworks displays the framework used in the project. Here we can see that Microsoft Net Core is the framework installed.

Packages folder shows currently installed packages, it also shows NuGet package references added for this project, which we will install later as we need them. In the package folder we can see that we have Microsoft.AspNetCore.SpaService.Extension is installed it is for supporting single page application in our case it is an Angular.

Properties

The properties node contains launchSettings.json file, this file only used when we use our 
application in local development machine or in debug mode. This file has nothing to do when we publish our application. 








Here we can see in the file that ASP NET Core environment is Development.

And also we have two profiles IIS Express profile this profile will be used whenever we launched our application in debug mod, it is related to internet information server. 

Other profile is related to our application FirstProject. This profile setting will be used whenever we start our application in console mode by running dotnet run command.

Controller

Which has been shipped with any MVC-based ASP.NET application   since the preceding release of the MVC framework.

Controller handles all incoming requests from the Client ( Angular in our case) and send response back to the Client (Angular) mostly in JSON format.





















Here we can see that WeatherForecastController is our controller name and it handles all the incoming requests destined for this controller. We will discuss more about Controller in upcoming articles.

Page

which contains a Razor Page file Error.cshtml to handle runtime and/or server errors. Whereas _ViewImports.cshtml file provides the mechanism to include directives globally.






Configuration

For the configuration of dot net core back end, such as modules, middleware compilation setting and project publishing we used three important files which are appsettings.json, Program.cs and Startup.cs.

appsetings.json: this file contains the setting information such as default connection string for connecting to the database.


Program.cs : dot net core mainly a console application and like every console application, Program.cs has a Main( ) method and this file is the starting point of our app.



















Main( ) method starts the CreateHostBuilder() then this method configure logging, dependency injection and then further configure  Kestral webserver also look at the configuration files such as appsettings.json and finally starts the Startup.cs file.

Startup.cs: This class is compulsory in dot net core application. Startup is a generic name we can give it any name to this file but we have to define it in webBuilder.UseStartup<>. 



















Startup class configures the Http request pipeline and utilize the Configure( ) method by adding the required middleware. 

This class also has a ConfigureServices( ) method and in this method we can add and configure required service and dependency injection.

Here is a YouTube video Link of my Dot Net Core Project Structure tutorial (Urdu/Hindi language)



Written By

Syed Sohail Ahmed Quadri

YouTube Channel



Introduction To Full Stack Dot Net Development

  WHAT IS MEAN BY FULL STACK DEVELOPER A software engineer who works both on the Front End like (Angular, HTML, JavaScript, CSS, Bootstrap,...