C# Variables, Comments & Data Types

Hello everyone,

Michael here, and in today’s post, we’ll explore C# variables, comments (notes that developers leave on code to explain it to other developers), and data types.

For many of the other programming tools we’ve covered on this blog’s journey like Python and Java, C# has its own ways of dealing with variables, comments, and data types.

In Python, here are some common variables, comments, and data types:

Variables

a = 3
b = 2

print(a+b)

5

Comments (single hashtag)

# This will print out the sum of 3+2

a = 3
b = 2

print(a+b)

5

Data types

complex
int 
str
bool 
dict

Likewise, in Java, here are some common variables, comments, and data types

Variables

String name = "Michael";
int year = 2024;

Comments

// This is a code comment
System.out.println("Hello world")

Data types

int
float
String
char

The variables, comments, and data types (C# version)

Now that we’ve reviewed variables, comments, and data types in Java and Python, let’s explore how these concepts work in C#.

Take this simple C# script, filled with variables, comments and data types:

// This code is writing a simple sentence with both string and int variables
int year = 2024;
string name = "Michael";
string month = "December";

Console.WriteLine(name + " is writing this post in " + month + " " + year);

Let’s examine this script line-by-line:

  • The top line with the double backslashes // signifies a comment being added to the script. Like Java and Python, C# comments are single-line deals.
  • int year = 2024; indicates that the int value in this script will be 2024
  • The two string variables in this script indicate that the two string values I will use in this script are Michael and December, respectively.
  • The Console.WriteLine() section of the script will print out my output to a command line-in this case, the string I want to output is Michael is writing this post in December 2024.
  • Another thing worth noting-to concatenate values in C#, use the + (plus) sign to do so. As you can see, I used the plus sign several times to concatenate my string values with other text in order to generate the following line of code: name + " is writing this post in " + month + " " + year.

Now that we’ve explored some basic examples of variables, data types, and comments in C#, what are some other data types in C#:

  • long-think of this as the 8-bit version of the int data type in C# (int is a 4-bit data type) that stores a broader range of integers
  • float-a 4-bit decimal type that stores values up to 6-7 decimal places
  • double-an 8-bit decimal type that stores values up to 15 decimal places
  • bool-the C# boolean type which stores true or false values
  • char-the C# character type which stores single characters
  • string-the C# string type which stores string values

Variables, Data Types, and Comments in C#: Things to Note

What are some important things to note about variables, data types, and comments in C# that are either similar to or perhaps quite different from variables, data types, and comments in other languages we’ve covered throughout this blog’s run?

  • In C#, comments are always indicated with the // (double backslash) symbol.
  • Just like in Java, you must specify a variable’s data type in the script (like I did with int year = 2024; for example).
  • Unlike Python, C# has no data types to handle complex/imaginary numbers (like 3+5i for example).
  • The boolean truth table rules that apply to languages like Java and Python also apply to C# (e.g. individual statements A & B must be true for a boolean statement to be true).

Also, here’s my script from this lesson on my GitHub-https://github.com/mfletcher2021/blogcode/blob/main/IntroToCSharp.cs.

Thanks for reading,

Michael

Leave a Reply