Basic Intro to Programing

By   Tewodros   Date Posted: Oct. 13, 2021  Hits: 956   Category:  ASP.NET C#   Total Comment: 0             A+ A-


side

Basic Programing Tutorial using C# Language

In this tutorial we will discuss basic programing concepts. We will mainly go through the basic datatypes and how we represent, define and store data in memory. 

 

Literals

Literals are any values we may want to store in memory. Example. 12, 55, “New York”, true or false, ‘A’, 4.99 etc. 

Datatypes

Datatypes are used to define the type of data we want to store in a variable. These the commonly used datatypes.

  1. short: stores small integer numbers like 5255
  2. long: stores big integer number like 4541245
  3. int: stores medium integer number like 1254, -562, 42555
  4. char: stores as single character like ‘A’  or ‘T’. Should be enclosed with single quote
  5. bool: stores a Boolean value (true or false)
  6. double: stores small to very big floating numbers/decimals. Eg. 25462455.25
  7. float: stores medium sized floating values like 5.25, 8245.65
  8. decimal:  stores financial data
  9. string: stores one or more characters like “John” or “New York”. Should be enclosed with double quotes

Variables

Variables are storage places in memory for a specific data we want to save to memory and retrieve it later. Variables are declared with the given datatype

When we declare a variable we need access modifiers, datatype and identifier. 

Identifier

Identifier means a valid name for your variable like store, account, student, i, n etc. Variable name can be combination of any alphanumeric character except some keywords that has special meaning by the C# language (like for, if, switch, struct, foreach)

Keywords

have special meaning to the C# language and are reserved to do special task like conditional branching, looping, or datatypes.

Example: all the datatypes are keywords

for, if, switch, struct, foreach, class, interface, abstract, extends are all keywords and cannot be used as our identifiers when we create our variables or class or interface..

Example of variable definitions. 

int i = 0;

Here int is the datatype, i is the identifier/variable name and 0 is the literal/value that is stored in memory under the name of i.

More Examples:

           int num1 = 10;

           short num2 = 500;

           long num3 = 6;

           float num4 = 30.05f;

           double num5 = 3.35;

           decimal num6 = 3.5M;

           bool flag = true;

           char c = 'a';

           string s = "Hello World!"; 

We can change the value of a variable by assigning a new value to it.

num1 = 20;

num5 = 30.35;

flag = false;

c = 'b';

s = “Hello”;

Single dimensional array  

Arrays are used to store one or more items of the same data type in memory. 

For example we want to store all prime number below 20.

int [] array = {1,3,5,7,11,13,17,19};

We may want to store names of best selling cars

string [] cars = {"Toyota", “Chevy”, “Ford”};

We may want to store all the English alphabets

char [] alphabets = {'a', ‘b’, ‘c’, ‘d’, 'e' ,'f'};   //you get the idea

If we don't know how many items we are going to store in the array, then we can take a guess of the maximum number of items (like 26 for English alphabets) or give some arbitrary big number. Any memory slot that we didn't use will be wasted so we need to be careful. 

int[] A= new int[3];

Here we create an array of integers who can store up to 3 numbers. Array indexing always start from 0 up to n-1, where n is the size of the array which is 3 in the above example. 

We can access arrays using angled brackets to put or retrieve values in to memory. 

A[0] = 1;

A[1] = 2;

A[2] = 3;

A[0]  retrieves 1 from memory for us. 

A[1] retrieves 2 from memory for us. 

If we know the items we want to store in the array then we can declare the array as follows. 

int[] A= new int {1,2,3};

string [] cars = {"Toyota", “Chevy”, “Ford”};

char [] alphabets = {'a', ‘b’, ‘c’, ‘d’, 'e' ,'f'};   

bool [] response = {true, false};

Two dimensional array

Two dimensional array store rows and columns of information of the same data type. The can also perceived as an array of arrays or matrix.

For example, the following table represents a two dimensional array with 3 rows and 4 columns.

11059
5234
6855

           int[,] array = new int[3, 4];

           array[0, 0] = 1;

           array[0, 1] = 10;

           array[0, 2] = 5;

           array[0, 3] = 9;

 

           array[1, 0] = 5;

           array[1, 1] = 2;

           array[1, 2] = 3;

           array[1, 3] = 4;

 

           array[2, 0] = 6;

           array[2, 1] = 8;

           array[2, 2] = 5;

           array[2, 3] = 5;

 

Constants

are variables but their values never change. In C# we can use the keyword const to declare a constant variable. A constant field also need a value we we declare it and we can't change the value after we assigned the value to it. 

const double PI =  3.14;

const double PI ; //error since we didn't assign a value to it.

const double PI =  3.14;

PI =  3.1415; //error since you cannot change the value of a constant after it is initialized. 

Enum

Enum are like arrays and are used to store a set of items which are under the same category like colors, responses, items but are stored and retrieved differently. Here is how we define our color enumeration. 

 enum Colors { white, red, green };

We can reassign/change the values of an array like A[0] = 10; but we cannot change enum values. So they are like constant arrays.

In order to access our enum we can use our . navigator like this

Console.WriteLine(Colors.white); //white

Struct

Struct are similar to a class but they don't support OOP concepts like inheritance, polymorphism, abstraction…

Class

A class is a a blue print of a real world entity like a student, class, account. It has attributes and set of behaviors/operations that we can do on those attributes. 

A class is created using the class keyword followed by the name of the class and preceded by the access modifier. Public means this class can used from outside of this class. More on access modifiers in another lesson. 

We have different types of classes

  1. Concrete Class
  2. Abstract Class
  3. Interface Class

Concrete Class

is a stand alone class which has concrete properties and methods. It can act as a parent class or a child class. We can instantiate a concreate class on it own. 

Abstract Class

an abstract class cannot stand on it own and hence we cannot instantiate (or create an instance abject from it).

An abstract class has its own concreate methods and properties but it must have at least one abstract method in which the child classes are forced to implement the details of it. 

Interface Type 

An interface type is like an abstract  class with just the template of a class and doesn’t implement the members that it defines. The derived/sub/child class of an interface can implement the members of the interface. 

Interfaces can contain methods signature and properties with no concrete implementation of methods. The interface method signature acts as a blue print for sub classes to implement. 

Access modifiers 

Access modifier dictates the scope of a variable. The scope means the part of our program where a variable is visible. You can only access the variable in the scope in which it is defined. This is sometimes within a method or a loop or a conditional statement block or a class. More on variable below. There are 5 access modifiers. 

  1. private : means the variable is only accessible inside the class
  2. internal: accessible inside a given namespace/package.
  3. protected: accessible inside the class and also to any sub class
  4. protected internal : accessible inside the class, to any sub class and also inside the same namespace
  5. public: Is accessible anywhere outside the class

Value and Reference types

Basically types can be classified as value type and reference types. 

The difference is value types occupy a copy of the data and is immutable means in order to change the value you have to abandon the variable completely and start with a new variable. Value type its create a unique copy of data. Each variable has its own spot in memory. 

However, reference types share a single copy of their data and if we make a change using one variable, then the value of the other variable will also change if they are pointing in to the same area in memory. 

All the datatype listed below are value types except string.

A String is a reference type even though it has most of the characteristics of a value type such as being immutable. Immutable means you cannot change the value of the variable.

Every time you assign a new value to a string it will create a new memory space for you.

 

 


Tags



Back to Top



Related Blogs






Please fill all fields that are required and click Add Comment button.

Name:*
Email:*
Comment:*
(Only 2000 char allowed)


Security Code:* belsyl

Back to Top