Mikael Olsson
C# 10 Quick Syntax Reference
A Pocket Guide to the Language, APIs, and Library
4th ed.
Logo of the publisher
Mikael Olsson
HAMMARLAND, Finland
ISBN 978-1-4842-7980-9 e-ISBN 978-1-4842-7981-6
https://doi.org/10.1007/978-1-4842-7981-6
Mikael Olsson 2022
This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
This Apress imprint is published by the registered company APress Media, LLC part of Springer Nature.
The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.
Introduction
The C# programming language is an object-oriented language created by Microsoft for .NET. C# (pronounced see sharp) builds on some of the best features of the major programming languages. It combines the power of C++ with the simplicity of Visual Basic and also borrows much from Java. This results in a language that is easy to learn and use, robust against errors, and enables rapid application development. All this is achieved without sacrificing much of the power or speed, when compared to C++.
In the years following its release in 2002, C# has grown to become one of the most popular programming languages. It is a general-purpose programming language, so it is useful for creating a wide range of programs. Everything from small utilities to computer games, desktop applications, or even operating systems can be built in C#. The language can also be used with ASP.NET to create web-based applications.
When developing in .NET, programmers are given a wide range of choices as to which programming language to use. Some of the more popular .NET languages include VB.NET, C++/CLI, F#, and C#. Among these, C# is often the language of choice. Like the other .NET languages, C# is initially compiled to an intermediate language. This language is called the Common Intermediate Language (CIL) and is run on .NET. A .NET program will therefore be able to execute on any system that has this framework installed.
.NET is a software framework that includes a common execution engine and a rich class library. It is cross-platform and can be installed on Windows, Linux,
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub (https://github.com/Apress). For more detailed information, please visit http://www.apress.com/source-code.
Table of Contents
About the Author
Mikael Olsson
is a professional web entrepreneur, programmer, and author. He works for an R&D company in Finland, where he specializes in software development. In his spare time, he writes books and creates websites that summarize various fields of interest. The books he writes are focused on teaching their subjects in the most efficient way possible, by explaining only what is relevant and practical without any unnecessary repetition or theory. The portal to his online businesses and other websites is Siforia.com .
About the Technical Reviewer
Doug Holland
is a Software engineer and architect at Microsoft Corporation and holds a masters degree in Software Engineering from the University of Oxford. Before joining Microsoft, he was awarded the Microsoft MVP and Intel Black Belt Developer awards.
Footnotes
https://docs.microsoft.com/en-us/dotnet/core/install/linux
https://docs.microsoft.com/en-us/dotnet/core/install/macos
www.mono-project.com
The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
M. Olsson C# 10 Quick Syntax Reference https://doi.org/10.1007/978-1-4842-7981-6_1
1. Hello World
Choosing an IDE
To begin coding in C#, you need an Integrated Development Environment (IDE) that supports .NET. The most popular choice is Microsofts own Visual Studio.
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# 10 is the current version and was released in 2021. Each version of the language corresponds to a version of Visual Studio, so in order to use the features of C# 10, you need Visual Studio 2022 (version 17.0 or higher). When installing Visual Studio, be sure to select the .NET desktop development workload in order to be able to develop with C#.
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 C# Console App (.NET Framework) template and click the Next button. Configure the name and location of the project if you want to and then click the Next button again. On this last page, make sure the latest version of .NET is selected. To use the features of C# 10, the project needs to target .NET 6.0 or later. 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 doubleclick on the file in the Solution Explorer in order to open it. In the source file, there is some basic code that you can go ahead and replace with the following code instead:
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.