You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
csharp-basics/1-introduction-to-visual-st...

6.9 KiB

marp paginate math theme title
true true mathjax buutti 1. Introduction to Visual Studio

Introduction to Visual Studio

Overview

  • Visual Studio An IDE (Integrated development environment) by Microsoft for Windows
  • Used for C#, C++, etc
  • There's a free Community edition
  • Multiple versions released
    • First version: 1997
    • 2017, 2019, 2022...

Features

Visual Studio vs Visual Studio Code (VS VS VSC)

  • Visual Studio is not to be confused with Visual Studio Code, a separate software by Microsoft
    • Visual Studio is out of the box a full-blown IDE with many built-in features
    • Visual Studio Code is more akin to text editors like Sublime Text, Atom etc.
      • Most features like IntelliSense or code completion have to be installed via plugins
  • While both can be used for many programming languages, for C# and ASP.NET Visual Studio is preferred due to its extensive debugging and build tools

Installing Visual Studio

  • Download Visual Studio Community (the free version) here
  • After launching the setup executable, you will be prompted to select the components to be installed along with VS
    • For our purposes you will need ASP.NET and web development found in the Workloads tab

  • Click Install in the lower right corner to begin installing VS and the required components
  • After installing VS, you can skip the account creation portion
  • Note: Installation size can be quite large (8 GB+) depending on which components are already installed on your system

Projects and solutions

  • In Visual Studio, there are two key concepts, projects and solutions
  • A project contains all files that are compiled into an executable application
    • Source code, images, data files, etc
    • Compiler settings, configuration files
    • In C#, handled by the project file .csproj by MSBuild
    • Creating a new project
  • A solution contains one or multiple projects
    • Build information, Visual Studio window settings
    • Files that aren't associated with a particular project
    • .sln is the solution file, .suo contains Solution User Options

Visual Studio UI

w:800

Default view in Visual Studio consists of three panels.

  1. Code editor
  2. Solution Explorer (CTRL+ALT+L) shows the files in the project
  3. Terminal (CTRL+Ö) for entering command line commands (e.g., for using Git)

Moving windows around

  • Drag and hold windows to move them around
    • Snap to layout window appears
  • After moving a window to the right, you can pin it to see its title on the right

Error highlighting in Visual Studio 2022

  • Visual Studio highlights errors in code automatically.

  • Why is this throwing an error?
  • \Rightarrow Missing one closing curly bracket!
  • Hovering on the red underline will show a hint for the error:
  • All errors can be displayed by pressing the red X at the bottom:

Keyboard shortcuts

  • CTRL+.
    • If a type is missing, use this to add the needed using directive
  • CTRL+R, then CTRL+R (seriously?)
    • Rename variable
    • Press Enter to confirm
  • CTRL+C (without selecting), then CTRL+V
    • Duplicate line
  • ALT+Up, ALT+Down
    • Move line up/down
  • Visual Studio Default keyboard shortcuts

Gotchas

  • Some actions are unavailable when your app is running
    • Deleting files
    • Adding new controllers

Exercise 1. Creating a console application

  1. Open Visual Studio 2022.
  2. Select Create a new project
  3. Type console to the search bar and select the following template:

Exercise 2. Running a console application

  1. To run your program, select Debug > Start Debugging, or press F5, or press the small ▶ sign next to your project name at the top
    • The template project should now run and print Hello World! to the console.
  2. Try changing the code so that it prints something else!

Reading