Chapter 2: Speaking C# [Flashcarder]
What is the primary focus of learning the C# language?
The primary focus is on understanding the grammar and vocabulary used daily to write the source code for applications.
How are programming languages similar to human languages?
Programming languages, like human languages, have their own grammar and vocabulary. However, unlike most human languages, in programming languages, you can create your own words.
What is the significance of creating your own ‘words’ in programming languages, as illustrated by the book?
Creating your own words in programming languages, as illustrated by Dr. Seuss’s creativity, highlights the flexibility and creative potential within programming to invent new concepts and functionalities.
For whom is the book’s section on C# programming language primarily written?
It is primarily written for beginners and covers fundamental topics every developer needs to know.
Up to which version of the C# language does the book cover features?
The book covers features of the C# language from version 1 up to the latest version, 12.
What kind of assistance does the book offer to those familiar with older versions of C# but interested in newer versions?
The book makes it easier for those familiar with older versions to learn about new features in recent versions by listing language versions and their important new features, along with chapter numbers and topics where they are discussed.
How does the book support learning about C# standards over the years?
It discusses various C# versions that Microsoft submitted to standards bodies, providing details on ECMA and ISO/IEC standards for C# versions 1.0, 2.0, 5.0, and 6.0.
Where can the reader find the latest C# standard document and additional resources on C# language design and compiler implementation?
The latest C# standard document and additional resources on C# language design and compiler implementation can be found on Microsoft’s official documentation site and public GitHub repositories as listed in the book.
What is the significance of the .NET SDK version in relation to the default C# language version used?
The .NET SDK version determines the default C# language version available for use, with each SDK version supporting a specific range of C# versions, from older versions up to the most current at the time of the SDK’s release.
What are the versions of C# that have been submitted to standards bodies, and what are their respective ECMA and ISO/IEC standards?
*Optional question
C# 1.0 (ECMA-334:2003, ISO/IEC 23270:2003),
C# 2.0 (ECMA-334:2006, ISO/IEC 23270:2006),
C# 5.0 (ECMA-334:2017, ISO/IEC 23270:2018),
C# 6.0 (ECMA-334:2022, ISO/IEC 23270:2022).
As of the book’s publication, which C# version’s ECMA standard is still a draft, and what is implied about the standardization of future versions?
The ECMA standard for C# 7.3 is still a draft, implying that C# versions 8 through 12 might not yet have ECMA standards.
When did Microsoft make C# open source, and where can the latest C# standard document be found?
Microsoft made C# open source in 2014.
The latest C# standard document can be found at https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/specifications.
What resources are more practically useful than the ECMA standards for learning and contributing to C# and related technologies?
The public GitHub repositories for C# language design (https://github.com/dotnet/csharplang), compiler implementation (https://github.com/dotnet/roslyn), and the standard to describe the language (https://github.com/dotnet/csharpstandard) are more practically useful than the ECMA standards.
How is the .NET language compiler for C# and Visual Basic distributed?
The .NET language compiler for C# and Visual Basic, also known as Roslyn, along with a separate compiler for F#, is distributed as part of the .NET SDK.
What determines the version of C# you can use?
To use a specific version of C#, you must have at least that version of the .NET SDK installed.
Can projects targeting older versions of .NET use modern compiler versions?
Yes, projects targeting older versions of .NET can still use a modern compiler version if you have a minimum required version of the .NET SDK installed.
For example, with the .NET 7 SDK installed, you can use C# 11 language features in a console app that targets .NET Core 3.0.
What is the relationship between .NET SDK versions and C# compiler versions?
Each .NET SDK version comes with a specific range of Roslyn compiler versions, which in turn supports a default C# language version.
How do .NET Standard versions relate to default C# compiler versions?
Similar to the .NET SDK, .NET Standard versions have associated default C# language versions, indicating the level of language features supported for targeting those standards.
How do you determine which version of the .NET SDK is installed on your machine?
Start Windows Terminal or Command Prompt on Windows, or Terminal on macOS, and enter the command: dotnet --version
What does the .NET SDK version 8.0.100 indicate?
It indicates the initial version of the SDK for .NET 8, without any bug fixes or new features added after its initial release.
What is assumed by developer tools like Visual Studio and the dotnet CLI regarding the C# language compiler version?
They assume you want to use the latest major version of a C# language compiler by default.
How can you enable improvements in a specific C# point release, like 7.1, 7.2, or 7.3?
Add a <LangVersion>
configuration element to the project file with the specific version you want to use, for example, <LangVersion>7.3</LangVersion>
.
If Microsoft releases a C# 12.1 compiler, how do you use its new language features?
Add a <LangVersion>
configuration element to your project file with the value 12.1.
What does setting <LangVersion>
to latestmajor do?
It uses the highest major number version of the compiler available, for example, version 12 in November 2023.
How does the latest setting differ from latestmajor in the <LangVersion>
configuration?
The latest setting uses the highest major and minor number version available, potentially enabling a minor version like 12.1 in the first half of 2024.
What is the effect of setting <LangVersion>
to preview?
It uses the highest available preview version of the compiler, like 12.0 in July 2023 with .NET 8 Preview 6 installed.
When did Microsoft release the first preview of .NET 9 with a C# 13 compiler?
In 2024.
How can you use the C# 13 compiler in your projects after installing the .NET 9 SDK preview?
By editing the .csproj file to add the <LangVersion>
element set to preview.
What does setting the <LangVersion>
to preview in your project file do?
It allows using the preview C# 13 compiler, enabling exploration of new language features.
Can you use a future version of the C# compiler, like C# 13, with .NET 8 projects?
Yes, you can use a future version of the C# compiler with .NET 8 projects by setting the <LangVersion>
appropriately in the project file.
What is the recommended practice for using preview versions of the C# compiler in projects?
Preview versions should only be set for exploration, not production projects, as they are not supported by Microsoft and are more likely to have bugs.
When is Microsoft expected to make the .NET 9 SDK generally available, and what does this mean for .NET 8 projects?
In November 2024, making it possible to use the C# 13 compiler in projects that continue to target .NET 8 by setting <LangVersion>
to 13.
What will be the default C# language version for projects targeting .NET 9 after installing its SDK?
C# 13 will be the default language version for projects targeting .NET 9.
What is a key consideration when using new C# language features with older versions of .NET?
Some new C# language features may require changes in the underlying .NET libraries, so not all new features can be used when targeting older versions of .NET.
What unique method is used to display the compiler version in the console app?
Add the #error version
statement in the Program.cs file to trigger a compiler error that reveals the compiler and language version.
What information is revealed by the compiler error CS8304?
The error reveals the compiler version (e.g., ‘4.8.0…’) and the default language version (12.0) as part of the error message.
What are the components of C# grammar?
C# grammar includes statements and blocks.
Additionally, comments are used to document code.
Besides comments, what are some methods to document your code in C#?
Besides comments, sensible naming for variables and functions, writing unit tests, and creating actual documentation are recommended practices for documenting code.
How does C# indicate the end of a statement?
C# indicates the end of a statement with a semicolon (;).
How is an English sentence structure analogy used to explain C# statements?
Just as the order of words in an English sentence (e.g., placing adjectives before nouns) matters for clarity and meaning, the order of types, variables, and expressions in C# statements is crucial for the code’s meaning and result.
What is an example of a C# statement, and what are its components?
An example of a C# statement is:decimal totalPrice = subtotal + salesTax;
This statement includes:
a type (decimal),
a variable (totalPrice), and
an expression (subtotal + salesTax) composed of operands (subtotal, salesTax) and an operator (+).
Why does the order of operands and operators matter in a C# expression?
The order affects the meaning and the result of the expression, similar to how word order affects meaning in natural language sentences.
What is the primary purpose of comments in code?
Comments are used to document code, increasing understanding of its functionality for others or for the original developer when revisiting the code later.
How do you add a single-line comment in C#?
Use a double slash //
at the beginning of the comment.
The compiler ignores everything after //
until the end of the line.
How can you write a multi-line comment in C#?
Use /*
at the beginning and */
at the end of the comment block.
Is it possible to use multi-line comment syntax for commenting in the middle of a statement? If so, how?
Yes, you can use /* and */ for commenting in the middle of a statement.
For example: decimal totalPrice = subtotal /* for this item */ + salesTax;
What should you consider if you find yourself adding too many comments in your code?
Consider if the code can be refactored to be more understandable without lengthy comments.
Well-designed code with clear function signatures and class encapsulation may reduce the need for extensive commenting.
How can you add or remove comment characters in Visual Studio 2022 and Visual Studio Code?
In Visual Studio 2022, navigate to Edit | Advanced | Comment Selection or Uncomment Selection.
In Visual Studio Code, navigate to Edit | Toggle Line Comment or Toggle Block Comment.
What is the difference between commenting code and commenting out code?
Commenting code involves adding descriptive text to explain code sections.
Commenting out code means adding comment characters to make code statements inactive.
Uncommenting means removing these comment characters.
How does C# indicate a block of code?
C# uses curly brackets { }
to indicate a block of code.
What can a block in C# define?
A block in C# can define the start and end of various language constructs, including namespaces, classes, methods, or statements like foreach.
What is a namespace in C# and why is it important?
A namespace in C# is used to organize code into groups, preventing naming conflicts and managing classes.
To access types in different namespaces, you need to use a using directive or a fully qualified name.
For example: using System;
What does a class in C# contain?
A class in C# contains the members of an object, including methods.
For example:
public class MyClass { public int Number; // Field public void Display() // Method { Console.WriteLine(Number); } }
What is contained within a method in C#?
A method in C# contains statements that implement an action that an object can take.
For example:
public void Greet() // Method { Console.WriteLine("Hello, world!"); }
How do code editors like Visual Studio and Visual Studio Code assist with blocks?
These code editors provide a feature to collapse and expand blocks by toggling the [-] or [+] symbols or an arrow symbol in the left margin of the code.
What are regions in C#?
Regions are defined labeled areas around any statements, allowing code editors to collapse and expand them similarly to blocks, helping to organize code into collapsible sections for better readability.
For example:
#region Methods public void Display() { Console.WriteLine(number); } #endregion
How can regions be used in code documentation and organization?
Regions can act as commented blocks that summarize what the block does, aiding in the documentation and organization of code, making it easier to navigate complex codebases.
How does the C# brace style compare to other languages like JavaScript?
C# typically places both the open and close braces on their own lines at the same indentation level, while languages like JavaScript often place the open brace at the end of the declaration statement.
Can you use different brace styles in C#?
Yes, you can use whatever brace style you prefer, as the compiler does not enforce a specific style. However, it’s easier to follow conventional style.
What is the most commonly used brace style in C#, according to Microsoft’s official documentation?
The brace style where both the open and close braces are on their own line and at the same indentation level is the most commonly used in C#.
What is advised regarding adherence to coding style conventions?
It’s recommended to follow the standards adopted by your development team or, if you’re a solo developer, to choose conventions that ensure your code is readable and maintainable, being kind to your future self by being consistent.
What types of characters are considered white space in C#?
The space, tab, and newline characters are considered white space in C#.
How does white space affect the C# compiler?
Extra white space has no effect on the compiler and can be used for formatting code for better readability.
What is the minimal white space requirement in C# statements?
The only required white space is between tokens that the compiler must recognize as separate, such as between a type and a variable name.
For example:string firstName = "Ben";
What are some examples of reserved keywords in C#?
Examples include:
using, namespace, class, static, int, string, double, bool, if, switch, break, while, do, for, foreach, this, and true.
What symbol characters are commonly used in C#?
Common symbol characters include:
“, ‘, +, -, *, /, %, @, and $.
How should C# keywords be formatted, and what is the convention for user-defined type names?
C# keywords are all in lowercase.
User-defined type names should not be all lowercase to avoid compiler warnings and future reserved keyword conflicts.
How many keywords does C# have compared to the number of distinct words in the English language?
C# has about 100 keywords, whereas the English language has more than 250,000 distinct words.
Why is C# considered difficult to learn despite having far fewer words than the English language?
The difficulty arises because developers need to learn and define new “words” or concepts within the language, expanding beyond the basic keywords to understand and utilize the language fully.
Why do programmers worldwide need to learn English?
Most programming languages, including C#, use English words for their syntax and keywords, such as “if” and “break.”
How do Visual Studio 2022 and Visual Studio Code visually differentiate C# keywords from other code?
By default, these IDEs show C# keywords in blue and other code in black, though this can be customized.
How can you customize the color scheme in Visual Studio 2022?
Go to Tools | Options, navigate to the Environment section, select Fonts and Colors, and then choose the display items to customize.
How do you change the color theme in Visual Studio Code?
Navigate to File | Preferences | Theme | Color Theme on Windows and Linux, or Code | Preferences | Theme | Color Theme on macOS, and select a color theme.
Why are plain text editors like Notepad not helpful for writing correct C# code?
Plain text editors do not provide feedback on syntax errors or code correctness, unlike specialized IDEs or editors with language support.
How do Visual Studio 2022 and Visual Studio Code help you write correct C# code?
They highlight spelling and grammatical errors in the code, such as method names that are incorrectly capitalized or statements missing a semicolon, using colored squiggly lines.
What is a namespace in C#, and why is it used?
A namespace is like an address that organizes code and helps to locate a specific method or type within larger libraries. It’s used to make code more manageable and to avoid name conflicts.
How do you import a namespace in C#, and what is its effect?
You use the using directive to import a namespace, e.g., using System;
.
This makes all types within that namespace available in your program without needing to specify the namespace prefix.
Why might importing namespaces be considered good practice in C#?
It simplifies the code by eliminating the need to prefix every type with its namespace, thereby making the code cleaner and more readable.
What problem do global using statements solve in C# 10 and .NET 6?
They reduce the need to include common using statements at the top of every .cs file by allowing a namespace to be imported once and made available throughout all .cs files in the project.
How can you implement global using statements in a project?
By adding global using statements in a dedicated file, such as GlobalUsings.cs, which contains all the global using statements for the project.
What is the benefit of using a separate file like GlobalUsings.cs for global using statements?
It organizes global imports in one place, making the project cleaner and easier to maintain by avoiding repetitive namespace imports across multiple files.
What naming convention is recommended for the file containing global using statements?
While the book suggests a practical approach by naming the file GlobalUsings.cs, it also notes that a de facto standard naming convention might emerge as developers get accustomed to this feature.
What does targeting .NET 6 or later with C# 10 or later automatically generate regarding global usings?
It generates a <ProjectName>.GlobalUsings.g.cs file in the obj\Debug\net<version> folder, which contains implicitly globally imported namespaces.</version></ProjectName>
How do implicitly imported namespaces vary?
The list of implicitly imported namespaces depends on the targeted SDK, with different SDKs adding specific namespaces to the implicitly imported list.
What namespaces are implicitly imported by the Microsoft.NET.Sdk and Microsoft.NET.Sdk.Web?
The Microsoft.NET.Sdk imports basic namespaces like System, System.IO, and System.Linq, among others.
Microsoft.NET.Sdk.Web includes: all from Microsoft.NET.Sdk plus web-specific namespaces like Microsoft.AspNetCore.Builder and Microsoft.Extensions.Hosting.
How can you view the autogenerated implicit imports file in a project?
By toggling on the “Show All Files” button in Solution Explorer and navigating through the obj -> Debug -> net8.0 folders to find the file named <ProjectName>.GlobalUsings.g.cs.</ProjectName>
What indicates that a file is autogenerated by the compiler in Visual Studio?
The file name includes a g for generated (e.g., <ProjectName>.GlobalUsings.g.cs), and the file itself starts with a comment indicating it is autogenerated.</ProjectName>
How do you use an alias for a class or statically import a class in the project file?
By specifying Alias or Static attributes in the <Using> element within the project file for the desired class, making it easier to reference in the code.</Using>
What is the effect of statically importing the Console class and aliasing the Environment class in a C# project?
Statically importing the Console class allows calling its methods without prefixing them with Console., and aliasing the Environment class enables referring to it with a shorter or more convenient name.
How can you disable the implicitly imported namespaces feature in a C# project?
By removing or setting the <ImplicitUsings> element to disable in the project file, allowing for manual management of global using statements.</ImplicitUsings>
How do methods in C# relate to verbs in English?
Just like verbs in English indicate action and change form based on tense, methods in C# perform actions and can change how they are called or executed based on the specifics of the action, a concept known as overloading.
Can you give an example of method overloading in C#?
Yes, the Console.WriteLine()
method is an example of overloading. It can be called with no arguments to output just a line terminator, or with arguments to output a string, formatted numbers, and dates.
What are the equivalents of nouns in C#?
In C#, equivalents of nouns are types, variables, fields, and properties.
How does C# categorize everything related to nouns?
Everything can be categorized as a type in C#, which further includes classes, structs, enums, interfaces, or delegates.
What is the difference between a type and a class in C#?
While a class is a specific kind of type in C#, the term “type” broadly refers to any categorization within C# that can include classes, structs, and other categorizations.
For example, string is a class, but int (which maps to System.Int32) is a struct.
How does C# define types?
C# itself only has a few keywords for types, such as string and int, which are aliases representing types provided by the .NET platform.
The platform offers a vast library of types available to C#.