C#: ASCII and Unicode

This entry is part 6 of 71 in the series C#

Traditionally, we used ASCII (American Standard Code for Information Interchange) characters to represent alphanumeric characters.

The ASCII was later extended to include another 128 characters. This allows some German characters such as ä, ö, ü, and the British currency symbol £ to be included.

However, not every language uses Latin letters. Chinese, Korean, and Thai are examples of languages that use different character sets.

Unicode attempts to include all characters in all languages in the world into one single character set.

Unicode Consortium endorses three character sets:

  • UTF-8  (Unicode Transformation Format, 8-bit encoding form). This is popular for HTML and for protocols whereby Unicode characters are transformed into a variable length encoding of bytes or 8 bits.
  • UTF-16  (Unicode Transformation Format, 16-bit encoding form). In this character encoding, all the more commonly used characters fit into a single 16-bit code unit. The .NET Framework uses this character encoding.
  • UTF-32  (Unicode Transformation Format, 32-bit encoding form). This character encoding uses 32 bits for every single character.

In HTML or web applications, we are using UTF-8 encoding.

If we create a HTML template, it will generally look like below showing that we are using UTF-8 character encoding:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

</body>
</html>

C#: Variables

This entry is part 7 of 71 in the series C#

Variables are just like the variables that we learnt in Mathematics when we were in the ninth grade or secondary school.

Every variable has a type that determines what values can be stored in the variable.

Below, we assign a value of 25 to a variable age that is of type integer (int).

int age = 25
Case-sensitive

Similar to the original C programming language, C# variable declaration is case-sensitive.

The declaration below will create 2 variables age and Age.

int age = 25
int Age = 25

C# is a type-safe language, and the C# compiler guarantees that values stored in variables are always of the appropriate type.

Variable must be definitely assigned

A variable must be definitely assigned before its value can be obtained.

It has to be declared and assign a specific value.

Commonly used variables
using System;

namespace Variables
{
    class Program
    {
        static void Main()
        {
            int point = 90;
            string greeting = "Hello, World!";
            double bigNumber = 1e100;

            Console.WriteLine("{0} {1} {2}", point, greeting, bigNumber);
            Console.ReadLine();
        }
    }
}

hworld

The above example shows the commonly used variable types integer, string and double.

In line 13, notice how we need to put the variables into array like {} in the writeline line to output the 3 variables.

The {0} denotes the first variable and {1} denotes the second variable and so on.

C#: Declaring implicit variables using var

This entry is part 8 of 71 in the series C#

Beginning in C# 3.0, variables can have an implicitly type var.

Consider the following 2 declarations which are functionally equivalent.

int i = 90 //explicitly declared as integer
var i = 90 //implicitly typed

An implicitly typed variable is strongly typed just as if you had declared the type yourself.  

The type will be determined by the compiler during compile time.

For example, we look at the following examples:

var firstNumber = 6;
var secondNumber = 3.5;
var thirdVariable = "This is a string!";

During compilation by Visual Studio, the compiler will determine that firstNumber is integer, secondNumber will be assigned as double and the thirdVariable will be assigned as a string.

Implicitly typed variables are strongly typed

When the compiler first see the following line

var a = 9

it will assign an integer to variable a.  If you later declare

a = 9.5 //9.5 is a double

The compiler will generate an error as it has already been determined to be an integer instead of a double.

 

C#: Learning An Object-Oriented Programming Language

This entry is part 9 of 71 in the series C#

C# is an object-oriented programming language.  This is very different from the traditional procedural languages such as C.

Its been said that if you have been a C programmer, it might take you 6 months to unlearn procedural style of programming and train yourself in writing programs the object-oriented style.

For a new programmer, the best approach is to learn an object-oriented language sucn as C# directly instead of learning a traditional language first.

The first thing is to understand that look at the world as if they are all objects.  For examples, a car, an animal or a company.

There are certain characteristics or properties that are associated with objects such as the size of a car or the number of employees in a company.

How do you manipulate or modify those properties?  You will use methods in a class to modify them.

In a way, you could say that an object is actually a class in an object-oriented language such as C#.

 

C#: namespace and class

This entry is part 10 of 71 in the series C#

Using the earlier simple program, we go through the components of a C# program.

using System;

namespace ConsoleHelloApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World");
            Console.ReadLine();
        }
    }
}

A namespace is a collection of classes.

Here in our simple program, there are actually 2 namespaces namely, System and ConsoleHelloApplication.

For the namespace ConsoleHelloApplication, we have only one class known as Program.

For the namespace System, we have used Console class to print out a line.

In an object-oriented program, we commonly say that Program class is a member of namespace ConsoleHelloApplication.

We can also say that the Main function is a member of Program class.

There is one more class here with 2 member functions.  Are you able to name the 2 member functions?

Well, the class is Console and the 2 member functions are WriteLine() and ReadLine().  The 2 member functions are also called the methods of Console class.