So friends, I'm learning C# and all things dotnet at the moment, so I thought I'd start to share my learnings with you in the format I'd learn best from. Follow along for snippets and quick answers to how to achieve different things in dotnet applications and solutions.
A variable is a tool or container we can use to store data or a value of a specific data type, that can then continue to be changed throughout a program. We can use the variable in various points in our program to access it's data/value for use in statements, expressions and more.
Declaring and initialising a variable
So let's declare our first variable together...
The first rule in C# is to declare the type of variable first (the type of data). Then second is where we give our variable a name. Here's an example...
string blogName;
Here we have only declared a variable. This variable cannot yet be used in our code or we cannot execute code which references this variable prior to assigning it a first value (initialisation).
So to initialise this variable, I may add a line of code, making it possible for me to later use the variable in my code for expressions, or in statements etc...
string blogName;
blogName = "LewisDoesDev";
I could simplify this by declaring and initialising my variable at the same time. This is also better practice as it makes my code more easily readable and by following this, we lessen the chance of uninitialised variables in our code.
Declaring variables without a type
It is also possible to use the var
keyword in C# to declare a variable. This doesn't explicitly declare the variable specifying its data type, and instead this requires the variable to be initialised at the same time as the complier will take the type of the first value assigned as the data type implied by the developer.
Here is an example...
var blogName = "LewisDoesDev";
It isn't possible for me to later assign a value with a different data type to the variable blogName. It is now declared and initialised as a string variable.
Data Types & Literals
So let's now discuss data types and literals in C#. A literal is simply a hard coded variable for example string firstName = "Lewis";
as opposed to a variable initialised or set with a value which uses an expression to determine the value for example another variable or a method.
Here's the high level on data types that're important to know when working with simple variables... (there are several more than the short list below including things like object, class, array, interface, enum and more. We may explore more complex types in later posts, or you can ask GitHub Copilot about those 😉).
-
string
is used for words, phrases or a mix of data types where perhaps the type isn't consistent and where we're not completing calculations. -
char
is for a single alphanumeric character -
int
is for a whole number. This cannot be used for decimal values. -
decimal
is for a number with a fractional component that is higher in precision with support for 28-29 digits. A 'm' should be appended to values. -
double
is similar to decimal except it is slightly less precise but uses less memory. There is no suffix to append here. 15-17 digits. -
float
is similar again except it is even less precise only allowing for 6-9 digits. Here we should append a 'f' suffix to values. This is a much faster option for numbers with a fractional component and hence more suitable for performance-critical logic or applications. -
bool
is a boolean data type which returns atrue
/false
value.
Test your knowledge 💡
Hopefully you learn't something with the content above. If you'd like to test your knowledge, use the toggles quiz below...
What are the rules for explicitly declaring a variable?
The data type must be stated first followed by the name of the variable in camel case. Next a value should ideally be assigned with initialises the variable. This looks something like this:
string blogName = "LewisDoesDev";
Can variables be declared without the data type keyword?
Yes! They can, and the first value assigned will be used as the data type for the keyword. This potentially makes code less easy to read though.
Which data type should be used for decimal calculations within logic that requires high performance?
Float would potentially be the best option here as it is the data type which performes the fastest, though it is the least precise. There is a tradeoff to consider here in whether to use a float, double or decimal.
Want to learn more? Here are some resources from Microsoft...
Thanks for stopping by this post. Hopefully this helped you with some of the basics around declaring and initialising variables in C# and the simple data types to go with them. If you'd like to continue learning or take advantage of some step by step guides, check out some of the Learn material from Microsoft below.