lunedì 4 luglio 2016

C# for n00bs, part one

Introduction to C# for N00bs who wants to start coding

It's a long time since the introduction of C# (read C Sharp) language at the beginning of the 00s. Sixteen years have passed since the introduction of .NET and C#, and the language is still in its evolution cycle. The last release, C# 6.0, was introduced just eleven months ago. 

C# is multi paradigm language as:
  • it's imperative
  • it's generic
  • it's object oriented
  • can be used for function programming
  • it's component oriented
also
  • it's strongly typed
  • it's declarative
Nearly all of these characteristics will be seen later in the article. C# is also an ISO Standard (ISO/IEC 23270:2006) and an ECMA Standard (ECMA-334), so everyone can develop its personal implementation.

It's all about data and instructions to use it

Nearly every programming language is based on two fundamental concepts: data and instructions. The first it's about "what" we're working with. The second one it's about "how and when" we're working with.

Just imagine a flow like this:
  1. Take a pot.
  2. Fill it with 250 ml of water.
  3. Place the pot on fire.
A developer will see the pot and the water as the data part of the flow, and the list above as the instructions. It's easy uh?

When developing a software, you need to "declare", aka tell the machine which data types to use and how to call them. You know, if you have one pot, it's just "pot". When you have seven pot, you must call them "Doc", "Grumpy", and so on because you need and unique identifier for each pot. Once "declared", data con be used in instructions. So you can "fill Grumpy with 250 ml of Water". Simply enough, uh?

Giving a first definition of "object" let's say it's the virtual correspondence to a real life object, like a pot, or a more abstract concept, like "communication" or "signal". An object is the sum of data and instruction used to define, or reproduce, the wanted behaviour of an element.

Introduction to data types

You've expected this. "Can I use only pots in my software?". Of course not. You have a set of basic types, the so called "simple" types, that are used to build more complex data. And you have objects, the complex data itself. 

Simple types

byte: the smallest numeric type. It's just, well, one byte, or 8 bits if you want. Can represent values just between 0 and 255. It's not signed, and it is one of the base types used in computer sciences. When you talk about data, you talk about one or aggregation of bytes.

int: it's a numeric type, represents a number between -2,147,483,648 and 2,147,483,647, with sign of course. It's a 32 bit numeric type. For now, just take it as a dogma.

long: it's another numeric type. It's just like int, but represents a number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. Long it's also signed as you can see. It's a 64 bit numeric type.

float: the first numeric type that supports fractional parts. It's used to say "floating point types", with the point used as usual for separation between decimal and fractional part. The point it's "floating" because the position is not fixed on the number. Example: 2.34 or 23.4. The point "floats" in memory. Float can represent numbers between ±1.5^−45 and ±3.4^38, with a precision of 7 digits. That's a lot. Float it's a 32 bit signed floating point number.

double: double it's just like float, but can represent numbers between ±5.0 × 10^−324 and ±1.7 × 10^308, with a precision of 16 digits. Double it's huge. It's a 64 bit signed floating point number.

string: friend of so much devs, a string represent the dev concept of "text". Strings store textual data in memory and allow text manipulation. It differs from other simple types for great degree, as it's also and object. But for now just take it, a string is a text bunch of data.

Complex data

You can combine the simple data types above to construct much more complex custom data types. These are structures, classes, enumerations and interfaces. We cannot talk now about them, as we must first cover other aspects of the language.

Special mention: namespaces

C# empathizes separation of concerns parading, which means that your program will be separated in small pieces of code built exactly to address separate concerns. This separation it's done not only at object level, it's obvious that a pot cannot be a car. It's also done using namespaces. Think at them like drawers. I will put in "Network" namespace all my objects related to network communications, and "Animations" everything related to animations in my graphic interface. So a namespace is a container used to organize and limit access to objects in your application.

It's all for now. Next time we will start coding the first very basic stuff. We will start talking about the basic structure of a program then start with some basic computations and data access. Enjoy!