Tanveer Najib
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Note: started on January 29th 2021. # **Conquering Visual C#** # Section 1 - Introduction ## Lesson 1.1 - What is programming and why should one learn it? **What is programming?** Before we start addressing this question, we’d like to demystify a huge myth about programming: Programming is not sitting in a cave all day wearing a Guy Fawkes mask (commonly known as hacker mask or anonymous mask), typing 0’s and 1’s in a terminal, and drinking fizzy drinks; just like programmers aren’t cavemen who spend all their lives doing nothing else other than spending hours and hours sitting in front of a computer, writing code. Just like painting, programming is a form of art that requires time and practice to master. Just like artists, programmers use their creativity combined with their skills to produce some sort of art. To summarize, computers don’t think like humans: they don’t have the capacity to think “outside of the box” like humans do, that is to say, computers have strict sets of rules that they must follow, in order to work. Computers are beings deprived of imagination and creativity. That’s where programming comes in handy: A programmer (the artist) uses their creativity and skills in order to produce art (software). You can think of programming as a way of giving a computer instructions, as a way of teaching the computer something, ranging from simple programs such as one that deletes a file to huge Neural Networks. **Why should I learn programming?** Normally, people would say that programming is worth learning because of how much money you can make out of it, but that’s not really a valid reason to learn programming, from our point of view. Imagine yourself sitting on a chair in front of a computer doing something you don’t even like for a wage. Regardless of how much your employer is willing to pay you, in the end it will just be an annoyance to you, one more thing that’ll make you feel even more stressed. The answer to this question (from our point of view) is that you should learn programming for the same reason why you probably learnt how to ride a bike during your childhood: Because you felt interested about it; because you felt like it would be fun to learn how to ride a bike. With programming, it’s exactly the same thing: You should learn it if you really are interested in computers and how they work. ## Lesson 1.2 - How does it work? Imagine yourself as a tourist in a foreign country and the computer as a native. You try to talk to them, but they don’t understand you. You try to use sign language, but they still don’t understand. Weird, right? What we mean by this is that a computer cannot directly understand your code: the only language a computer understands is machine code (commonly known as binary) therefore needing an intermediary to translate your code into machine code. There are two mutually exclusive types of intermediaries, that work in distinct ways: compilers and interpreters. **Compiler** In a compiled language, such as C and C++, programs must be compiled before being executed, that is to say, the program must be translated into machine code that the computer can understand. The compilation process works in the following way: The compiler examines your code, translating it into machine code, wrapping that translation in a file that can be directly executed by the programmer. This makes the program run faster (as the computer can directly understand the language) and the startup time bigger (because the code has to be examined and compiled before being executed). <center> <img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116184/compilation_usvrih.png"></img> *Flowchart demonstrating how a compiler works.* </center> **Interpreter** Instead of translating all the code into machine code at once, an interpreter translates the line at the same time it is being executed thus making the program slower and the startup time smaller (as no examination is performed). <center> <img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116178/unnamed_wiwpj1.png"></img> *Flowchart demonstrating how an interpreter works.* </center> ## Lesson 1.3 - Installing Visual Studio Before we get started with the fundamentals of programming, we need to first establish what we will be using to actually write code. That’s what an IDE is used for! IDE stands for Integrated Development Environment, or in other words, a piece of software that allows you to write software, giving you a set of tools to boost your productivity, such as syntax highlighting, code completion, etc. Simple, isn’t it?! In this book, we will be using Microsoft Visual Studio 2019 to write and execute our code. If you want, you can use other versions of Visual Studio, like 2017 or 2015, but if you want to follow along with this book, it is recommended that you use 2019. **Installing the IDE** To get started head over to https://visualstudio.microsoft.com/downloads/ and click “Free Download“ underneath Community edition. You can use Enterprise or any other versions, but for the sake of this tutorial, we’ll use Community. <center> <img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116857/vs-dl-page_ql47jy.png"></img> </center> <br>Once that has downloaded, open it up and you should see a screen with a list of components. You don’t need all of these components, just the “.NET desktop development“ one for now. <center> <img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116878/net-comp_jguce1.png"></img> </center> <br>Go ahead and click install after checking “.NET desktop development“. This will install all the core features of Visual Studio, and install support for C# development. What did we do in this lesson? We installed Visual Studio 2019, ready for some actual programming! **Tasks** 1. Go ahead and get used to Visual Studio. *Notes* _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ ## Lesson 1.4 - Creating your first project In order to start programming, we need to first create our environment, or to put it simply, our project. Visual Studio provides us with a handful of pre-created templates for us to use, for the sake of this tutorial, we will be using the simple Console App template. This template provides us with a simple command line interface, that looks a bit like the Windows Command Prompt. *Task: Open Command Prompt on your PC. Note how the application looks.* **Starting a new project** Open Visual Studio 2019. You should be greeted with a page like this: <center> <img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116883/devenv_f6nhln.png"></img> </center> <br>We are faced with a few options: **Clone a repository**: This allows you to edit code stored on a repository. This will be explored later on in Section {x}. **Open a project or solution**: This one’s pretty self-explanatory. It opens a project that you have already created. Visual Studio uses the .sln file format. **Open a local folder**: This will allow you to open a folder on your hard drive, so you can edit the files within it. **Create a new project**: This option allows you to create a new project with a given template **Continue without code**: This will open Visual Studio, but will not open any projects <br>For this lesson, we will be using the fourth option, “Create a new project”. Once clicked it should show a screen like this: <center> <img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615117441/templates_cnqafq.png"></img> </center> <br>Using the search function at the top, type “console app”. As you start typing, you should see the results get added to the panel below. As mentioned before, we will be using the Console App template, so the option we are looking for is Console App (.NET Framework). **NOTE**: Make sure to choose the one with the C# icon, not the VB one. VB (Visual Basic) is a different language from C#. Once you double click that option it will show you a project configuration screen: <center> <img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116872/settings_q3utwq.png"></img> </center> <br>In the Project name text box (marked with a 1), type something meaningful, like “MyFirstApp”. To choose where to store the location of the project click the button with the three dots (marked with a 2). It is recommended that you create a folder on your harddrive called “C# Projects”, so you can store all your projects in one location. The Solution name is what you want to call the solution, this should be the same as your Project name. After you have entered all those details, go ahead and click “Create” (marked with a 4). Visual Studio will then create a new project, based off of your configuration, and choice in project template (console app). <center> <img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116881/ide_cnlqvf.png"></img> </center> <br>That should be it! You have created your very first Visual C# project! The above image shows you what your screen should look like. It might seem daunting at first, but don’t worry, we’ll go through everything you need to know about this screen. **The Solution Explorer** On the left of your IDE window, you should see a tab called Solution Explorer. <center> <img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116869/solution-explorer_ebukfm.png"></img> </center> <br>This is the tab that shows all the files within your “solution”. If you can’t see this tab, go to View, then click Solution Explorer. One thing to keep in your notes is that a “solution” is a collection of projects. We’ll get to what this actually means in another lesson. As you can see in the screenshot, the project has a couple of files. The one that is most important is the file called “Program.cs”. This is the file where we will be writing our code. **The Text Editor** <center> <img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116866/text-editor_s24cxn.png"></img> </center> <br>In the middle we have the text editor (see above). This is where we will be writing our code. By default, we have the Program.cs file open. At the top of the window we have a set icons: <center> <img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116885/bar_bdw4gn.png"></img> </center> <br>Most of these are pretty self explanatory, such as “Open” or “Save”. During the next few lessons you will mainly be using: 1. **Save all:** Saves all files in a project.2 2. **Start:** Builds and runs your program. Go ahead and click the “Start” button. Note that nothing seems to happen, this is because we haven’t actually written any code. We will be doing that in Section 2. What did we do in this lesson? We created our very first console app project! We explored all the necessary tabs in Visual Studio! **Tasks** 1. Create a new Console App called MySecondApp. Store it in the same directory as MyFirstApp. *Notes* _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ # Section 2 - Basics of Programming ## Lesson 2.1 - A brief introduction You might be wondering “When will I actually start writing code?”, and the answer is: right now. We’ve got everything set up, ready to start writing some C# code! Let us start by the code automatically generated by Visual Studio upon the project’s creation. **Analyzing automatically generated code** ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyFirstApp { public class Program { public static void Main(string[] args) { } } } ``` There’s a lot to unpack here, so let’s get this started! A programming language ie. **C#**, **C++**, **Python**, is just a set of tools that allow you to communicate with your computer. Programming languages have a reserved set of words called keywords, ie. for, int, void, double, that produce a defined behaviour when used. Keywords cannot be used for naming variables, functions, etc... It’s also noticeable that almost every line of code terminates with a semicolon. We’ll talk about this more in depth later. **Namespaces** In C#, the project source code (the code that constitutes a project) is grouped into one or several namespaces, with the intent of making the source code more organized. In this case, the project’s source code is stored in the namespace “MyFirstApp”. In order to access namespaces we use the using keyword. Ever had a dossier? If so, probably you used separators at some point. Think of separators as your namespaces and the content within the separators the actual source code. As explained before, namespaces can be accessed through the using keyword in the following syntax: using NAMESPACE; so let us look at the first statement. ```csharp using System; ``` It’s easily deducible that we’re using the namespace System or, in order words, we’re telling the programming language we need access to the code within the namespace System. Namespaces can also be chained, meaning you can have as many namespaces you want within another namespace. The next using statements are an example of chained namespaces. ```csharp using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; ``` ## Lesson 2.2 - Hello World! In this lesson, you’re going to write your first line of code! Open your “MyFirstApp” project (double click the MyFirstApp.sln file). If needed, open the Program.cs file from your solution explorer. This is the file that will hold all your code for now. In the future, you may have multiple files that do different things. It all depends on the size and scope of your project. Let’s not worry about this for now, we’ll go more in depth about it in a future lesson. For this lesson, we’ll keep it simple. We’ll be making the legendary “Hello World” application. The “Hello World” project is what most beginner programmers start off with. For most, it’s the first line of code they’ve ever written. In the text edit, insert the following code: ```csharp using System; namespace MyFirstApp { public class Program { public static void Main(string[] args) { //Your code goes here Console.WriteLine("Hello, world!"); } } } ``` So, what does this code do? Well, it's pretty self-explanatory. The line `Console.WriteLine("Hello, World!");` writes 'Hello, World!' to the console. If we go ahead and run this code, we'll see that it seems to do nothing. This is because we're not waiting for user input or anything like that, so the program writes 'Hello, World!', but then closes the program immediately, giving the effect that it isn't doing anything. In order to fix this, we need to include the line `Console.ReadLine();` beneath it, like this: ```csharp //Your code goes here Console.WriteLine("Hello, world!"); Console.ReadLine(); ``` `Console.ReadLine();` waits for the user to type something into the console until they press enter. If you run the code now, you'll see that the phrase 'Hello, World!' has been written to the console, and the program will now wait for you to press enter before closing. **Tasks** 1. Try writing a bit about yourself! For example your name, age, occupation, etc. Try doing this with multiple `Console.WritLine();` *Notes* _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ _________________________________________________________________ ## Lesson 2.3 - Variables You're probably already familiar with the concept of variables. Variables are one of the most useful concepts in programming: They allow you to reference a certain value by a name that you can choose. The syntax is as simple as this: ```csharp DATATYPE VARNAME = VALUE; ``` Where: + **DATATYPE:** The type of data that the variable holds (such as `int`). + **VARNAME:** The variable name. Must obbey to a set of rules that will be mentioned later. + **VALUE:** The value that the variable will hold. Must be consistent with **DATATYPE**. If we were to create a variable of type `int` (integer), the syntax is: ```csharp int var1 = 25; ``` In this example, we assigned the value `25` to a variable called `var1` of type `int`. What if we wanted to show the user what the value of `var1` is? Simple! Just use the previously-explained `Console.WriteLine();` statement! ```csharp Console.WriteLine(var1); ``` This will output the value of `var1` that, in this case, is equal to `25`. **Variable naming rules** Variable naming **must** obey a set of naming conventions. + Variable names cannot start with number. + Certain special characters, such as ", ', ., $ cannot be used in a variable name. + Make sure to give your variables a descriptive (and short) name, in order to make your code more readable

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully