C# 8 Quick Syntax Reference: A Pocket Guide to the Language, APIs, and Library
()
About this ebook
In the C# 8 Quick Syntax Reference, you will find a concise reference to the C# language syntax: short, simple, and focused code examples; a well laid out table of contents; and a comprehensive index allowing easy review. You won’t find any technical jargon, bloated samples, drawn-out history lessons, or witty stories. What you will find is a language reference that is to the point and highly accessible. The book is packed with useful information and is a must-have for any C# programmer.
What You Will Learn
- Discover what's new in C# 8 and .NET for Windows 10 programming
- Employ nullable reference types
- Explore the advanced async streams now available in C# 8
- Work with ranges and indices
- Apply recursive patterns to your applications
- Use switch expressions
Who This Book Is For
Those with some experience in programming, looking for a quick, handy reference. Some C# or .NET recommended but not necessary.
Mikael Olsson
Mikael Olsson is a professional programmer, author and web entrepreneur. He enjoys teaching, writing books and making sites that summarize various fields of interest. The books he writes are focused on teaching their subject in the most efficient way possible, by explaining only what is relevant and practical without any unnecessary repetition or theory. He can be reached online at Siforia.com.
Read more from Mikael Olsson
Handbook of Success: How to Make your Life What you Want it to Be Rating: 5 out of 5 stars5/5C++17 Quick Syntax Reference: A Pocket Guide to the Language, APIs and Library Rating: 0 out of 5 stars0 ratingsJava Quick Syntax Reference Rating: 0 out of 5 stars0 ratingsJava 17 Quick Syntax Reference: A Pocket Guide to the Java SE Language, APIs, and Library Rating: 0 out of 5 stars0 ratingsCSS3 Quick Syntax Reference: A Pocket Guide to the Cascading Style Sheets Language Rating: 0 out of 5 stars0 ratingsPHP 8 Quick Scripting Reference: A Pocket Guide to PHP Web Scripting Rating: 0 out of 5 stars0 ratingsCSS Quick Syntax Reference Rating: 0 out of 5 stars0 ratingsC# 7 Quick Syntax Reference: A Pocket Guide to the Language, APIs, and Library Rating: 0 out of 5 stars0 ratingsModern C Quick Syntax Reference: A Pocket Guide to the Language, APIs, and Library Rating: 0 out of 5 stars0 ratingsC++20 Quick Syntax Reference: A Pocket Guide to the Language, APIs, and Library Rating: 0 out of 5 stars0 ratings
Related to C# 8 Quick Syntax Reference
Related ebooks
C# 7 Quick Syntax Reference: A Pocket Guide to the Language, APIs, and Library Rating: 0 out of 5 stars0 ratingsModern C Quick Syntax Reference: A Pocket Guide to the Language, APIs, and Library Rating: 0 out of 5 stars0 ratingsC++17 Quick Syntax Reference: A Pocket Guide to the Language, APIs and Library Rating: 0 out of 5 stars0 ratingsIntroducing Vala Programming: A Language and Techniques to Boost Productivity Rating: 0 out of 5 stars0 ratingsC++20 Quick Syntax Reference: A Pocket Guide to the Language, APIs, and Library Rating: 0 out of 5 stars0 ratingsWeb Applications with Elm: Functional Programming for the Web Rating: 0 out of 5 stars0 ratingsPHP 8 Quick Scripting Reference: A Pocket Guide to PHP Web Scripting Rating: 0 out of 5 stars0 ratingsBeginning Perl Programming: From Novice to Professional Rating: 0 out of 5 stars0 ratingsC# Deconstructed: Discover how C# works on the .NET Framework Rating: 0 out of 5 stars0 ratingsReact and Libraries: Your Complete Guide to the React Ecosystem Rating: 0 out of 5 stars0 ratingsPro ASP.NET MVC 5 Platform Rating: 0 out of 5 stars0 ratingsExploring the .NET Core 3.0 Runtime: Through Code Generation and Metadata Inspection Rating: 0 out of 5 stars0 ratingsPython Bees Rating: 0 out of 5 stars0 ratingsVisual Studio Code Distilled: Evolved Code Editing for Windows, macOS, and Linux Rating: 3 out of 5 stars3/5Practical Oracle JET: Developing Enterprise Applications in JavaScript Rating: 0 out of 5 stars0 ratingsAbsolute Beginner's Python Programming: The Illustrated Guide to Learning Computer Programming Rating: 1 out of 5 stars1/5Pointers in C Programming: A Modern Approach to Memory Management, Recursive Data Structures, Strings, and Arrays Rating: 0 out of 5 stars0 ratingsPerl 6 Quick Syntax Reference: A Pocket Guide to the Language, the Core Modules, and the Community Rating: 0 out of 5 stars0 ratingsOracle APEX Best Practices Rating: 0 out of 5 stars0 ratingsMastering C: A Comprehensive Guide to Programming Excellence Rating: 0 out of 5 stars0 ratingsBeginning Rust Programming Rating: 0 out of 5 stars0 ratingsDomain-Specific Languages in R: Advanced Statistical Programming Rating: 0 out of 5 stars0 ratingsBeginning Linux Programming Rating: 0 out of 5 stars0 ratingsBuilding REST APIs with Flask: Create Python Web Services with MySQL Rating: 0 out of 5 stars0 ratingsPro TypeScript: Application-Scale JavaScript Development Rating: 4 out of 5 stars4/5Engineering a Compiler Rating: 0 out of 5 stars0 ratingsLearn to Program with Kotlin: From the Basics to Projects with Text and Image Processing Rating: 0 out of 5 stars0 ratingsRobot Operating System (ROS) for Absolute Beginners: Robotics Programming Made Easy Rating: 0 out of 5 stars0 ratingsVue on Rails: End-to-End Guide to Building Web Apps Using Vue.js and Rails Rating: 0 out of 5 stars0 ratings
Programming For You
SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5A Slackers Guide to Coding with Python: Ultimate Beginners Guide to Learning Python Quick Rating: 0 out of 5 stars0 ratingsExcel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Programming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Mastering Windows PowerShell Scripting Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Python for Beginners: Learn the Fundamentals of Computer Programming Rating: 0 out of 5 stars0 ratingsPython: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsC# Programming from Zero to Proficiency (Introduction): C# from Zero to Proficiency, #0 Rating: 0 out of 5 stars0 ratingsHacking Essentials - The Beginner's Guide To Ethical Hacking And Penetration Testing Rating: 3 out of 5 stars3/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsHow to Learn PHP, MySQL and Javascript Quickly!: For Dummies Rating: 5 out of 5 stars5/5Web Designer's Idea Book, Volume 4: Inspiration from the Best Web Design Trends, Themes and Styles Rating: 4 out of 5 stars4/5
Reviews for C# 8 Quick Syntax Reference
0 ratings0 reviews
Book preview
C# 8 Quick Syntax Reference - Mikael Olsson
© Mikael Olsson 2020
M. OlssonC# 8 Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-5577-3_1
1. Hello World
Mikael Olsson¹
(1)
HAMMARLAND, Finland
Choosing an IDE
To begin coding in C#, you need an Integrated Development Environment (IDE) that supports the Microsoft .NET Framework. The most popular choice is Microsoft’s own Visual Studio.¹ This IDE is available for free as a light version called Visual Studio Community, which can be downloaded from the Visual Studio web site.²
The C# language has undergone a number of updates since the initial release of C# 1.0 in 2002. At the time of writing, C# 8.0 is the current version and was released in 2019. Each version of the language corresponds to a version of Visual Studio, so in order to use the features of C# 8.0, you need Visual Studio 2019 (version 16.3 or higher).
Note
When installing Visual Studio, be sure to select the .NET desktop development
and .NET Core cross-platform development
workloads in order to be able to use C# 8.0.
Creating a Project
After installing the IDE, go ahead and launch it. You then need to create a new project, which will manage the C# source files and other resources. To display the New Project window, go to File ➤ New ➤ Project in Visual Studio. From there, select the Console App (.NET Core)
template and click the Next button. Configure the name and location of the project if you want to and then click Create to allow the project wizard to create your project.
You have now created a C# project. In the Solution Explorer pane (View ➤ Solution Explorer), you can see that the project consists of a single C# source file (.cs) that should already be opened. If not, you can double-click the file in the Solution Explorer in order to open it. In the source file, there is some basic code to help you get started. However, to keep things simple at this stage, go ahead and simplify the code into this.
class MyApp
{
static void Main()
{
}
}
The application now consists of a class called MyApp containing an empty Main method, both delimited by curly brackets. The Main method is the entry point of the program and must have this format. The casing is also important since C# is case-sensitive. The curly brackets delimit what belongs to a code entity, such as a class or method, and they must be included. The brackets, along with their content, are referred to as code blocks, or just blocks.
Hello World
As is common when learning a new programming language, the first program to write is one that displays a Hello World
text string. This is accomplished by adding the following line of code between the curly brackets of the Main method .
System.Console.WriteLine(Hello World
);
This line of code uses the WriteLine method, which accepts a single string parameter delimited by double quotes. The method is located inside the Console class, which belongs to the System namespace. Note that the dot operator (.) is used to access members of both namespaces and classes. The statement must end with a semicolon, as must all statements in C#. Your code should now look like this.
class MyApp
{
static void Main()
{
System.Console.WriteLine(Hello World
);
}
}
The WriteLine method adds a line break at the end of the printed string. To display a string without a line break, you use the Write method instead.
IntelliSense
When writing code in Visual Studio, a window called IntelliSense will pop up wherever there are multiple predetermined alternatives from which to choose. This window is very useful and can be brought up manually by pressing Ctrl+Space. It gives you quick access to any code entities you are able to use within your program, including the classes and methods of the .NET Framework along with their descriptions. This is a very powerful feature that you should learn to use.
Footnotes
1
www.visualstudio.com
2
www.visualstudio.com/vs/community/
© Mikael Olsson 2020
M. OlssonC# 8 Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-5577-3_2
2. Compile and Run
Mikael Olsson¹
(1)
HAMMARLAND, Finland
Visual Studio Compilation
With the Hello World program completed, the next step is to compile and run it. To do so, open the Debug menu and select Start Without Debugging, or simply press Ctrl+F5. Visual Studio will then compile and run the application, which displays the string in a console window.
Console Compilation
If you did not have an IDE such as Visual Studio, you could still compile the program as long as you have the .NET Framework installed. To try this, open a console window (C:\Windows\System32\cmd.exe) and navigate to the project folder where the source file is located. You then need to find the C# compiler called csc.exe, which is located in a path similar to the one shown here. Run the compiler with the source filename as an argument and it will produce an executable in the current folder.
C:\MySolution\MyProject>
\Windows\Microsoft.NET\Framework64\v3.5\
csc.exe Program.cs
If you try running the compiled program from the console window, it will show the same output as the one created by Visual Studio.
C:\MySolution\MyProject> Program.exe
Hello World
Language Version
As of Visual Studio 2019, the default version of C# used is determined by which version of .NET the project targets. You can change this version by right-clicking the project node in the Solution Explorer and selecting Properties. From there, under the Application tab, there is a drop-down list labeled Target framework. To use the latest features from C# 8.0, the project needs to target .NET Core 3.0 or later so make sure it is selected. For this option to be available, the .NET Core workload must have been selected when installing Visual Studio 2019 and the project must be created using one of the .NET Core templates.
Comments
Comments are used to insert notes into the source code. C# uses the standard C++ comment notations, with both single-line and multi-line comments. They are meant only to enhance the readability of the source code and have no effect on the end program. The single-line comment begins with // and extends to the end of the line. The multi-line comment may span multiple lines and is delimited by /* and */.
// single-line comment
/* multi-line
comment */
In addition to these, there are two documentation comments. There is one single-line documentation comment that starts with /// and one multi-line documentation comment that is delimited by /** and */. These comments are used when producing class documentation.
///
class MyApp
{
/**
args>Command line arguments.
*/
static void Main(string[] args)
{
System.Console.WriteLine(Hello World
);
}
}
© Mikael Olsson 2020
M. OlssonC# 8 Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-5577-3_3
3. Variables
Mikael Olsson¹
(1)
HAMMARLAND, Finland
Variables are used for storing data in memory during program execution.
Data Types
Depending on what data you need to store, there are several different kinds of data types. The simple types in C# consist of four signed integer types and four unsigned, three floating-point types, as well as char and bool.