Let's learn about value and reference types, null, and why C# is considered to be "strongly-typed"!
The first and most important thing to know about C## as a programming language is this: C## is a strongly-typed _language. This means that every variable, every constant, every class, every single object ever created using C## has a _type. It is impossible for an object to exist in C## without it having a type.
A type in C## (and .NET) is a set of properties about a specific kind of object. These might include the storage space it needs, the maximum or minimum size of the object, and others.
int four = 4; //Max value 2147483647 (2^31 - 1), Min value 2147483648 (-2^32) double twopointfive = 2.5; //Size: 8 bytes char a = 'a'; //Size: 16 bit
The lines above demonstrate how to create a variable in C#. The first word in each line above is the type (e.g.
char), the second word is the variable name (e.g.
a) and the number or character on the right side of the
= is the variable's value.
Because C## is a strongly-typed language, C## is also a type-safe language. That means that objects which are instantiated as a given type (number, character, date, class, etc.) have rules in place to ensure that said instance behaves as that type. Consequently the C## compiler will allow us to add two number types together, but trying to add a number to a word will cause a error.
int five = 5; int ten = five + five; //No problem! int invalidTen = five + "five"; //Compilation error!
Let's continue our C# in Simple Terms series with one of the basic parts of any line of code: the operators.
Now that we understand a little more about classes and previously learned the difference between value types and reference types, it's time to explore some more specialized C# types. In today's edition of C# in Simple Terms, let's explore two useful value types: structs and enums.
In the past, we have used mega-series to tackle big subjects such as design patterns, anti-patterns, and sorting algorithms. In this series, we're going back to basics to discover, learn, and teach the programming language we all know and love: C#!
Now that we've discussed most of the basics we need for a C# program, let's talk about two concepts that are central to how C# (and indeed, all object-oriented programming languages) work: inheritance and polymorphism.
In this article, the latest of our C# in Simple Terms series, we're going to discuss how to control the flow of execution in a C# program. This means we will answer the question, "how does the code know what to do next?"