c#
Chapter 0: Creating your first program 'Hello World'
Table Of Contents
Introduction to course:
Hello. Welcome to the C# course, the goal of this course is to take a non-programmer from not knowing any programming language to being proficient in C# (not that I want you to leave, if you do know other programming languages, just know that it may be a little slower than otherwise would be).
This course will be broken up into chapters, each teaching a new concept, then the future chapters will build on the previous chapters and through careful inclusion of previous concepts, spaced reptation, practice, and lastly, allowing free easy lookups of past material. The end of the course will have a final project and quiz that will test your understanding of the entire course.
Each chapter will include a video designed to give broad overview of the concept, a text detailed description of the concept and finally an assignment to practice the concept.
C# is a programming language developed by Microsoft. Built with heavy influence from Java, so often people consider the syntax (formatting) similar, which means after this course, Java would be the next easiest language for you to pick up. Normally Visual Studios is used to write C# to allow quick and easy building of the forms and tightly coupling of coding and form creation to allow for quick development of fully programmable forms. This makes C# an ideal language for forms, when using windows development/production environment, though Microsoft is pushing more capabilities to Visual Studio for Mac, so will be able to be developed on either environment.
Video summary:
Setting up
Install Visual Studio. (I will be demoing using Visual Studio 2022 Community.)
During installation on workload page click to include '.NET desktop development'
Choose the theme. (I chose Dark mode, because I like it the best)
Create a new project.
Choose C# as the language. (If no results show up click 'install more tools and features' and select to add '.NET desktop development', then click 'Modify')
Choose 'Windows Forms App' then click next.
Choose a name for the project. (I chose HelloWorld). Click Next.
Choose Framework. (I chose .NET 6.0). Click Create.
Getting familiar with the basic sections of Visual Studio:
First along the top is the menu bar.
Menu Bar, (along the top), used when managing the project overall.
Quick access bar, right under menu bar, used for actions taken often while creating and testing the project.
Toolbox, hidden to just the name on the left, is used in adding items to the screen.
Data source, hidden to just the name on the left, is used with databases, will cover in more detail later.
Main section, currently contains the what is new dialog. Will contain the form or code, and will be main spot for us to work in.
Solution explorer, on the top right, contains the project files, to allow switching from and between files in the project.
Properties, on the bottom right, contains the properties (such as name, value, etc.) for what is currently selected in the main section.
Status bar, along the bottom, shows the current state of Visual Studio and what is happening.
Menu Bar along the top
File, has everything related to saving, importing, and exporting. Basically what has to do with the file system (hard drive).
Edit, has everything to do with code changes, copy, paste, find, and replace. Basically when you want to edit code, without looking at line by line.
View, has to do with what windows are shown on the screen. Basically how you view the Visual Studios.
Git, deals with sharing the code with the team with collaboration tool called Git. Don't worry about this until later.
Debug, deals with all the options for running the code in debug mode, to allow for easier identification of mistakes. Basically used when there is a bug to debug the program.
Analyze, used to make Visual Studio Analyze the project, clean up code and automation of what used to be manual tedious work. Will cover more in depth later.
Tools, where we add functionality to Visual Studio such as adding the workload '.NET desktop development'. Whenever something is possible, but not showing up in Visual Studio, see if a tool needs to be added.
Extensions, allows third party additions of functionality to Visual Studio. Don't worry about this.
Window, allows modification of how the windows appear and where they display. Will cover more about this later, for now will leave it as default.
Help, see version and other resources to assist where loss for where or how to do something.
Search, allows for quick search to find things.
Our first program 'Hello World':
This is a follow along exercise, to allow students to learn as you go. All actions will be explained in the parentheses.
Follow Setting up steps if not already done. (Setting up installs the software, workload, and creates the first project, this allows the computer to be in a state to create and run the program).
2. On the left side click the 'Toolbox'. (This is where all your components Visual Studio has predefined to allow easy adding on the main screen.
3. Click to expand common controls. (These are the most common form elements used).
4. Click and drag the element called 'Label' onto the middle of the form. (Labels are used to describe other elements or to display output to the user).
5. Scroll to the Text property in the properties section. (Properties carries the information of how the element looks and behaves.)
6. Change the value next to text from 'label1' to 'Hello World'. (This changes the text property value to 'Hello World', the text property is what is displayed to the user when the see the element).
7. Run the program, by clicking the green arrow, with the name of the project in my case 'HelloWorld'. (Running the program compiles and runs the program just as if a user would run the program. Often it is better to test and run the program more often, especially as a new programmer, so you can see what every change does.)
8. View the results. (As we can see the text Hello World was added to the screen and we have the basic form controls like minimize, expand, and exit built for us).
9. Close the form by pressing the X in top right. (This returns us back to where we can make more changes to the program).
We are almost done, but since this is learning to program course, let's write our first line of code.
10. Double click anywhere white on the form. (This takes us to the code side of the form and creates an event (action) handle that acts on form load.
11. Enter the code label1.Text = "Hello " + Environment.UserName; between the brackets in Form1_load.
label1 refers to the element with the 'name' property set to label1 (the default name for first label added to the screen).
.Text refers to the Text property of the element that it is directly left of it, in this case the label1's Text property
= Sets the property on the left to the value on the right. (Note all math is done before setting the value).
"Hello " is a String that contains the letters 'H', 'e', 'l', 'l', 'o', and ' ' (yes the space is a letter). Strings are always just a series of characters.
Environment Is referring to the environment the program is being run in, in this case my computer.
.UserName is referring to the Environments UserName, in this case, what is the UserName of my computer, the logged in user's UserName. (In my case 'Jimmy M Patton'.
; Indicates the end of a line, this is mandatory. Because this lets us control the white spacing and lets there be a visual indication where the end of a line is. Just get in the habit of adding it every time you hit enter with anything on the line.
To summarize, the code first looks at the element label1's Text property and set the value to ("Hello " + The logged on user's UserName)
12. Finally lets see the result, just to see what it does. Run the program again, just like in step 7. (This is to verify we have typed everything correctly and verify our program is finished.)
13. Congratulations, you have officially written your first program in C#. Give yourself a pat on the back.
Assignment:
Create a program that when ran does:
Displays a greeting to the user who logs on (displaying the user's username). (Can be any greeting except Hello.)
Displays the greeting and name in Font of 22 or larger. (Hint: Use the Font Property on the label to change the Font size.)
Display a copyright message along the bottom of the screen.
Example:
Summary:
We installed Visual Studio Community edition 2022, with .NET Development Desktop. We created our first hello world program. We modified that program to display the user's username on form load. You created a more advanced one, with larger font and an extra label, to show mastery of skills shown in the 1st lesson.
Overall, very good work this week. Next week, we will cover inputs, and how we get and use inputs from the user.