Monday, April 01, 2024

Master the fundamentals of C# 12 programming, how to use variables, data types, and constants in your projects?

In the previous lesson, we created our first C# 12 program. We learned how to print text to the Terminal panel and explored the three types of comments used in the C# programming language. Additionally, we discovered how to set attributes in the *.csproj file, which affects the entire project. We also delved into creating regions, which allow us to organize code into blocks and navigate through it easily, regardless of the number of lines of code.

Furthermore, despite the convenience of C# 12’s top-level statements, we can still create programs with the traditional Main method that you might recognize from older versions of the C# programming language. However, everything we’ve learned so far would only suffice for printing text on consoles. Programming encompasses much more than that! Let’s first focus on what programming is and what it involves.

A boy is learning C# 12 programming at home

A boy is learning C# 12 programming at home to make a game

Programming is the process of creating computer programs, where programmers use a specific language and tools to communicate with the computer. Programming is the art of writing code that enables computers to perform specific tasks and Programmers use various languages (such as C#, Python, Java, etc.) to write instructions for the computer. In any case, programming requires creativity and problem-solving skills. It goes beyond mere code writing. It involves creating solutions, thinking about problems, and communicating with computers to achieve desired functionality.

Programmers often need to devise innovative ways to tackle challenges. But how do computers actually function? Programming involves giving instructions to the computer in the form of one or more grouped lines of code, known as statements. These statements are grouped into methods, and methods into classes, to avoid code repetition in a project and enable calling previously written and tested code from other parts of the project. 

The code you write must have a purpose and be written with a significant reason. It should also be correct and optimized. Your computer will always execute exactly what you've instructed, regardless of whether it's logical. Logic and proper code writing are up to you. Learning programming usually begins with console applications. Although console applications aren’t used for creating market-ready 
programs due to their purely textual user interface, they are excellent for testing methods and classes.

Understanding Declarations, Variables, Data Types, and the Role of Constants

Every program typically starts with declaring and assigning variable values, which requires knowledge of data types—specifically, the data types in the C# programming language. Variables are fundamental elements of any program, allowing programs to process data by storing and modifying it during program execution. In C#, variables are declared by specifying their name and data type.

The variable name can contain letters, numbers, and underscores, but it cannot start with a number, special characters or contain spaces. Also, you cannot name your variable the same as a C# keyword. It’s essential to give descriptive names to variables so that their purpose in the program is easily understood. For example, declaring an int variable representing a numeric value in C# looks like this:

int number = 43;

This declaration creates an int variable named number and assigns it the value 43. The variable number can now be used in the program to perform various operations with numbers. However, when you see this variable, you don't know what its value 43 represents in this case. That's why, for example, it's better to name the variable like this:

int dictionaryGerman = 43;

In this way, we assume that the variable contains a value that means that the bookstore has 43 dictionaries. That is much clearer. Note that C# is a case-sensitive programming language, which means that variable names with different capitalization are considered distinct. Always use camel case for variable names. But what are constants? In computer programming, a constant is a value that cannot be altered by the program during normal execution.

Once associated with an identifier, a constant remains fixed, and its value does not change. Constants are used for quantities that remain the same across different aspects of computation. They can be stored at specified memory locations in the computer. Unlike variables, which can vary, constants maintain their value throughout the program’s execution. In C# programming, constants are defined using the const keyword. Constants play a crucial role in ensuring stability and predictability in programs.

const decimal fee = 13,47 // monthly account maintenance fee

Understanding Data Types in C#: A Comprehensive Overview

Data types are categories used to describe the kinds of data used in computing, programming, and other fields. A data type specifies the type of value that can be stored in a variable or object, as well as the possible operations that can be performed on that value. In the C# programming language, there are two fundamental types: vale types and reference types. Unlike some other languages, C# does not use pointers but rather reference types.

This distinction is important, especially if you’re familiar with programming languages like C or C++. C# is also strict in variable declaration, which means it is strongly typed. In C#, each variable must have an explicitly defined data type, and during program compilation, this type is rigorously checked. Additionally, variables need to be initialized; otherwise, an error will be reported during coding in the IDE - Integrated Development Environment. Let’s explore the data types available in the C# 12  programming language.

Data Types in C# 12 programming language

Data Types in C# 12 programming language

These types can be used to define variables and method parameters in the C# 12 programming language. Additionally, there’s the possibility of defining custom data types using classes or structures. This way, you can easily create your own data types for your projects. To explore the limitations of a specific data type and how to represent it, take a look at the following illustrative program. So, create a new program, name it “datatypes” and type the following code:

/// <summary>


///  MANUEL RADOVANOVIC - 2024-03-18


/// </summary>


using static System.Console;


WriteLine(new string('-', 100));





WriteLine(new string('-', 100));

WriteLine("VALUE TYPES");

WriteLine(new string('-', 100));



// This bool type of variable can be true or false

bool boolVariable = true;

WriteLine("bool or System.Boolean ... {0}, {1}", boolVariable, !boolVariable);


// This byte type of variable has a range from 0 to 255

byte byteVariable = 0;

WriteLine("byte or System.Byte ... {0}, {1}", byteVariable, byteVariable = 255);


// This sbyte type of variable has a range from -128 to 127

sbyte sbyteVariable = 0;

WriteLine("sbyte or System.SByte ... {0}, {1}", sbyteVariable, sbyteVariable = 127);


// This char type of variable has only a character

char charVariable = 'A';

WriteLine("char or System.Char ... {0}", charVariable);


decimal decimalVariable = decimal.MinValue;

WriteLine("decimal or System.Decimal ... {0},\n\t\t\t\t {1}\n", decimalVariable, decimalVariable = decimal.MaxValue);


double doubleVariable = double.MinValue;

WriteLine("double or System.Double ... {0},\n\t\t\t  {1}\n", doubleVariable, doubleVariable = double.MaxValue);


float floatVariable = float.MinValue;

WriteLine("float or System.Single ... {0},\n {1}\n", floatVariable, floatVariable = float.MaxValue);


int intVariable = int.MinValue;

WriteLine("int or System.Int32 ... {0}, {1}\n", intVariable, intVariable = int.MaxValue);


uint uintVariable = uint.MinValue;

WriteLine("uint or System.UInt32 ... {0}, {1}\n", uintVariable, uintVariable = uint.MaxValue);


long longVariable = long.MinValue;

WriteLine("long or System.Int64 ... {0}, {1}\n", longVariable, longVariable = long.MaxValue);


ulong ulongVariable = ulong.MinValue;

WriteLine("ulong or System.UInt64 ... {0}, {1}\n", ulongVariable, ulongVariable = ulong.MaxValue);


short shortVariable = short.MinValue;

WriteLine("short or System.Int16 ... {0}, {1}\n", shortVariable, shortVariable = short.MaxValue);


ushort ushortVariable = ushort.MinValue;

WriteLine("ushort or System.UInt16 ... {0}, {1}\n", ushortVariable, ushortVariable = ushort.MaxValue);



WriteLine(new string('-', 100));


WriteLine(new string('-', 100));



string stringVariable = "A string can have 2GB or about maximum 1,073,741,823 characters.";

WriteLine("string or System.String ... {0}\n", stringVariable);


object objectVariable = "An object can have any type of variable.";

WriteLine("object or System.Object ... {0}\n", objectVariable);



WriteLine(new string('-', 100));


WriteLine(new string('-', 100));



WriteLine("class ... User-defined data types.");

WriteLine("interface ... Specification that classes can implement.");

WriteLine("array ... Sequence of values of a data type.");

WriteLine("delegate ... Reference to a method.\n");


WriteLine("In addition to these, there are also complex types such as");

WriteLine("structures, enumerations, dynamic, tuples, Half, etc.\n");



WriteLine(new string('-', 100));


WriteLine(new string('-', 100));



WriteLine("Const is not data type!");

WriteLine("You cannot change the constant value after initialization.");


// a const

const decimal PI = 3.14159265359M;


WriteLine("Constant PI = {0} always. ", PI);

When you complete the above program, you will get the following result:






bool or System.Boolean ... True, False

byte or System.Byte ... 0, 255

sbyte or System.SByte ... 0, 127

char or System.Char ... A

decimal or System.Decimal ... -79228162514264337593543950335,


double or System.Double ... -1.7976931348623157E+308,



float or System.Single ... -3.4028235E+38, 3.4028235E+38

int or System.Int32 ... -2147483648, 2147483647

uint or System.UInt32 ... 0, 4294967295

long or System.Int64 ... -9223372036854775808, 9223372036854775807

ulong or System.UInt64 ... 0, 18446744073709551615

short or System.Int16 ... -32768, 32767

ushort or System.UInt16 ... 0, 65535




string or System.String ... A string can have 2GB or about maximum 1,073,741,823 characters.

object or System.Object ... An object can have any type of variable.




class ... User-defined data types.

interface ... Specification that classes can implement.

array ... Sequence of values of a data type.

delegate ... Reference to a method.

In addition to these, there are also complex types such as

structures, enumerations, dynamic, tuples, Half, etc.




Const is not data type!

You cannot change the constant value after initialization.

Constant PI = 3.14159265359 always.

You can see how the entire program was created and coded in the following video, too.

C# 12 - 3. What Data Types are there in C# 12 ?


No comments:

Post a Comment