visit
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
In the code above, the using
keyword tells the compiler that we would like to access functionality defined in a particular namespace
. The analogy that I like to use here is that a namespace
is like a folder structure, and when we are asking the compiler to use things from a namespace
it’s like having access to that folder. Of course, this is an analogy, but I think about it this way from a code organization perspective. The System
namespace
provides many fundamental things that we’ll be accessing in most of our C# programs.
After this, we actually define our very own namespace
called “MyFirstProgram”. As you can see, we have some curly braces after that and code that gets nested inside of those curly braces. So this will mean that all of the code we put inside will belong to the namespace
“MyFirstProgram”.
We define a new class
called “Program” inside of the namespace, where a class
is used to group functionality into a particular type
. The file-folder analogy I started with begins to break down a little bit more here, but you can think about a class
almost like a file in one of your folders (i.e. your namespace
). In this particular code example, the “Program” class
is the entry point to the program we are writing, so when your program starts up it will look for the static
method
called “Main” on your “Program” class
. The string[] args
portion that you see is a parameter (in this case, an array of command-line arguments) that can be passed to our program.
Finally, inside the Main method, we use the Console.WriteLine
method to write the text “Hello, World!” to the console. So if you think about a command prompt window, this program just outputs those words for you to see. And that’s it!
Console.WriteLine("Hello, World!");
int myNumber = 1337;
This example code shows an integer (int
as the first word written there) that we are declaring with the name “myNumber”. We also assign the value of 1337 to that integer. So this shows a declaration of a variable AND an assignment put together. You could separate these onto multiple lines if you wanted:
int myNumber;
myNumber = 1337;
using System;
namespace VariablesAndDataTypes
{
class Program
{
static void Main(string[] args)
{
int myNumber = 1337;
float myFloat = 133.7;
double myDouble = 133.7;
bool myBool = true;
string myString = "Hello, World!";
}
}
}
Here’s an example of an “if statement” that contains else
branches as well that will determine if a number is positive, negative, or zero:
int num = 5;
if (num < 0)
{
Console.WriteLine("The number is negative.");
}
else if (num > 0)
{
Console.WriteLine("The number is positive.");
}
else
{
Console.WriteLine("The number is zero.");
}
int num = 5
string result = num switch
{
> 0 => "The number is positive.",
< 0 => "The number is negative.",
_ => "The number is zero.",
};
Console.WriteLine(result);
for
loops: Used to execute a code block a specific number of times.while
loops: Used to execute a code block repeatedly, as long as a given condition is true.do-while
loops: Used to execute a code block repeatedly, until a given condition is no longer true.foreach
loops: Used to iterate over the elements of a collection or array.
Let’s have a look at some example code that uses a for
loop to iterate through an array (i.e. a collection) of integers:
int[] numbers = { 1, 2, 3, 4, 5 };
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
In the code above, our for
loop starts at 0 using an integer variable called i
that will increment by 1 each iteration of the loop but only while the value of i
is less than the length of the array. And if you’re wondering why we start at zero and only while it’s less than the length of the array, that’s because the first position in an array is actually index 0! It takes some getting used to, but many programming languages are zero-based like this, so position 1 is actually index 0.
We could write similar code using a foreach
loop:
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int num in numbers){
Console.WriteLine(num);
}
In programming, methods and functions refer to code that we can label with an identifier and reuse throughout a program. In C#, we define methods and functions within a class
(or if you’re using those fancy then they can exist outside of a class
) and they are composed of:
private
, so only within the current class
).void
if there is no return type.
int AddNumbers(int num1, int num2)
{
return num1 + num2;
}
In the above example, because there is no access modifier at the start of the method signature, it will be private
. This method will return an integer, is named “AddNumbers”, and takes two integer parameters (num1
and num2
). When we look at the actual body of the method, we can see that it is adding the two numbers together, and then we have the return
keyword that says we’ll be providing the sum of those two numbers to the caller of this method as the result.
We’ve already seen a couple of examples of these, but a class
is the fundamental building block of object-oriented programming in C#. I used the analogy of a file in a file system earlier when referring to how we’d organize them within namespaces
, but a class defines the properties and methods of a certain type of object.
When we have defined the functionality of a class
, we can create a new instance of that class to be used using the new
keyword. This allocates into memory a new reference of the class
for us to work with. The exception here is when we deal with the static
keyword, but that’s homework!
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public void PrintInfo()
{
Console.WriteLine("Mm name is " + Name + " and I am " + Age + " years old.");
}
}
That means if we made more than one instance of the class, we could have two different Person
references with different values:
Person p1 = new Person();
p1.Name = "Nick"
p1.Age = 33;
Person p2 = new Person();
p2.Name = "Jamal";
p2.Age = 18;
p1.PrintInfo();
p2.PrintInfo();
Running the code above would print out information for two different Person
objects!
Read the .