DEV Community

Cover image for The C# Chronicles-Week 0
Jo
Jo

Posted on • Edited on

The C# Chronicles-Week 0

I have started taking C# classes at my local coding bootcamp in the hopes of expanding my skills into becoming a fullstack developer at my current company. I've decided to document my process to:

  • Keep myself accountable
  • Keep track of my notes-look back to as reference
  • Develop my skills as a technical writer

The first class dealt with environment and all that fun jazz. This was relatively smooth for me, despite being on a mac. Actually this was smooth because I set up the software before class. Always nice to be prepared. We learnt some of the basics of C# as well as how to get it running from Visual Studio (in lieu of Visual Studio Community). If you would also like to run c# from Visual Studio, search for c# in your extensions. Ta-da!

I learnt how to run a simple c# program by running 'dotnet run' in the terminal.
In order to create a new console app, we run the command 'dotnet new console -n Primitive Types', where -n is the name of the app; the name of the app in this case being "PrimitiveTypes". This command will input all the files and folders that come with creating a new console app.

I've circled the files that come with the initial set-up that appears when you create a new console app.

In order to run your code, type 'dotnet run' in your command line or terminal.

Basic Types

One thing I like about c# is that it is a strictly types language, meaning that you can set your type where you can set your variables to be integers, strings etc. For example:

int myFavNumber=27;
string myName="Jo";
Enter fullscreen mode Exit fullscreen mode

This is saying that this variable is an integer. This is called implicit versus explicit versus implicit type casting. So in c#, if you use the var keyword instead of the int, then this is implicit typing, as you are asking the program to figure out what the type is. Explicit is me telling the program what I want my type to be. Whereas you can change a 'var' in c# (much like javascript), you cannot change 'const' (also much like javascript)

There are several different number types in C#. What changes for each type is the amount of memory they take up and the specificity of each. Decimals, for example take up much more memory and specificity than floats.
Ex:

float myFavFloat=3.14f;
decimal myFavDec=100.01m;
Enter fullscreen mode Exit fullscreen mode

The f and m here helps to indicate the type of number.

Basic Types: Math with Ints

You can do math with ints, just be careful what type you use since specificity does apply in the case of division. So:
decimal MyFavNum=10m/3m will give a more specific result than int MyFavNum=10/3

Strings

Strings are another data type. Here is something interesting about the string type. If you declare a string type using sing quotes, it will be defined as a char (pronounced car) which is a different data type. In javascript, for example, it does not matter if you use double or single quotes.

We can concatenate (or add strings together) a couple different ways in c#.

string myFavColor="Yellow";
string mySecFavColor="Purple";
Console.WriteLine(myFavColor + " "+ mySecFavColor);
Enter fullscreen mode Exit fullscreen mode

The result here will be "Yellow Purple"

Another way to concatenate strings is by variable interpolation where we use a dollar sign and wrap variable names in curly braces.

string myFavColor="Yellow";
string mySecFavColor="Purple";
Console.WriteLine($"{myFavColor} + " "+ {mySecFavColor}");
Enter fullscreen mode Exit fullscreen mode

Booleans

Booleans are true and false. To declare a boolean,we:
bool isDone=true;

Control Flow

  {
            Console.WriteLine("Hello World!");
            Console.WriteLine("What is your name?");
            string name = Console.ReadLine();
           if(name==""){
                Console.WriteLine("I didn't hear you");
          } 
           else if(name=="Jo" && 7>5){
            Console.WriteLine("Hello Boss");
          }
           else{
                  Console.WriteLine($"Hello {name}");
            } 
       }
Enter fullscreen mode Exit fullscreen mode

Here we are introduced to the Console.ReadLine method, where we can accept a user's input. The control flow is also seems similar to javascript's control flow, even the syntax is very similar.

Control Flow-Switches

Instead of several else-if statements, we can use a switch statement to offer multiple results to a user. In this example, we use a switch statement to provide the user with a different answer based on the day of the week.

Console.WriteLine("Hello World!");
            Console.WriteLine("What day of the week is it?");
            string input=Console.ReadLine().ToLower();

            switch(input){
                case "monday":
                Console.Write("5 days till the weekend");
                break;

                case "tuesday":
                Console.Write("Tuesday blues but you can do it!");
                break;

                case "wednesday":
                Console.Write("Happy humpday!");
                break;

                case "thursday":
                Console.Write("Almost friday!");
                break;

                case "friday":
                Console.Write("tgif amirite");
                break;

                case "saturday":
                 case "sunday":
                Console.Write("bottomless mimosas");
                break;

                default:
                Console.WriteLine("I don't understand that input");
                break;
            }

            Console.WriteLine("Bye!");

        }
Enter fullscreen mode Exit fullscreen mode

To improve the use of switches, we can use defaults and fallthroughs. We insert a default at the end of our switch, which would act as our final 'else', when none of our previous conditions are met. The fallthrough in this case is case "saturday", where you want to provide the user with the same result, regardless of answer. In this case, since it is the weekend, we want the answer to be "bottomless mimosas whether it is Saturday or Sunday.

Control Flow: For Loop

A for loop is a way to run a block of code over and over again till a condition is met

Console.WriteLine("Hello World!");
           for (int i=0; i < 10;i++) 
            { 
           if(i==3){ 
              Console.WriteLine("i is equal to " + i); 
              continue;
            } 

              Console.WriteLine("i is not 3 it is " + i); 

           } 

           Console.WriteLine("end");
Enter fullscreen mode Exit fullscreen mode

The continue statement says, "When you hit this line, start the loop over, continue onto the next iteration"
The break statement allows us to say if we've found what we've looking for, go ahead and stop the loop.

Control Flow-While Loop

We use the while loop to to continue running the same code and it can run indefinitely till stopped. It's often used to continue running an application until a certain condition is applied. A while loop should never equate to false.

 int i=0; 
          while(i<100){
          i++;
            if(i%2==0){
             continue;
             } 
             Console.Write(i);
Enter fullscreen mode Exit fullscreen mode

Here ends Week 0!

Top comments (0)