Skip to main content

2018 | Buch

C# 7 Quick Syntax Reference

A Pocket Guide to the Language, APIs, and Library

insite
SUCHEN

Über dieses Buch

This quick C# 7 guide is a condensed code and syntax reference to the C# programming language, updated with the latest features of C# 7.3 for .NET and Windows 10. It presents the essential C# 7 syntax in a well-organized format that can be used as a handy reference.

In the C# 7 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 concise, 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 LearnDiscover what's new in C# 7.3 and .NET for Windows 10 programming, including out values, ref locals and returns, local functions, throw exceptions, and numeric literals
Create lightweight, unnamed types that contain multiple public fieldsCreate branching logic based on arbitrary types and values of the members of those types
Nest functions inside other functions to limit their scope and visibility
Throw expressions in code constructs that previously were not allowed
Declare methods with the async modifier to return other types in addition to TaskUse the new numeric literals found in C# 7.3 to improve readability for numeric constants
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.

Inhaltsverzeichnis

Frontmatter
Chapter 1. Hello World
Abstract
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 website.
Mikael Olsson
Chapter 2. Compile and Run
Abstract
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.
Mikael Olsson
Chapter 3. Variables
Abstract
Variables are used for storing data in memory during program execution.
Mikael Olsson
Chapter 4. Operators
Abstract
Operators are special symbols used to operate on values. They can be grouped into five types: arithmetic, assignment, comparison, logical, and bitwise operators.
Mikael Olsson
Chapter 5. Strings
Abstract
The string data type is used to store string constants. They are delimited by double quotes.

                string a = "Hello";
              
Mikael Olsson
Chapter 6. Arrays
Abstract
An array is a data structure used for storing a collection of values that all have the same data type.
Mikael Olsson
Chapter 7. Conditionals
Abstract
Conditional statements are used to execute different code blocks based on different conditions.
Mikael Olsson
Chapter 8. Loops
Abstract
There are four looping structures in C#. These are used to execute a code block multiple times. Just as with the conditional if statement, the curly brackets for the loops can be left out if there is only one statement in the code block.
Mikael Olsson
Chapter 9. Methods
Abstract
Methods are reusable code blocks that will only execute when called.
Mikael Olsson
Chapter 10. Class
Abstract
A class is a template used to create objects. They are made up of members, the main two of which are fields and methods. Fields are variables that hold the state of the object, while methods define what the object can do.
Mikael Olsson
Chapter 11. Inheritance
Abstract
Inheritance allows a class to acquire the members of another class. In the following example, the class Square inherits from Rectangle, specified by a colon. Rectangle then becomes the base class of Square, which in turn becomes a derived class of Rectangle. In addition to its own members, Square gains all accessible members in Rectangle, except for any constructors or destructors.
Mikael Olsson
Chapter 12. Redefining Members
Abstract
A member in a derived class can redefine a member in its base class. This can be done for all kinds of inherited members, but it is most often used to give instance methods new implementations. To give a method a new implementation, the method is redefined in the child class with the same signature as it has in the base class. The signature includes the name, parameters, and return type of the method.

                class Rectangle
                {
                  public int x = 1, y = 10;
                  public int GetArea() { return x * y; }
                }
               
                class Square : Rectangle
                {
                  public int GetArea() { return 2 * x; }
                }
              
Mikael Olsson
Chapter 13. Access Levels
Abstract
Every class member has an accessibility level that determines where the member will be visible. There are six of them available in C#: public, protected, internal, protected internal, private, and private protected, the last of which was added in C# 7.2. The default access level for members of a class is private.
Mikael Olsson
Chapter 14. Static
Abstract
The static keyword can be used to declare fields and methods that can be accessed without having to create an instance of the class. Static (class) members only exist in one copy, which belongs to the class itself, whereas instance (non-static) members are created as new copies for each new object. This means that static methods cannot use instance members since these methods are not part of an instance. On the other hand, instance methods can use both static and instance members.
Mikael Olsson
Chapter 15. Properties
Abstract
Properties in C# provide the ability to protect a field by reading and writing to it through special methods called accessors. They are generally declared as public with the same data type as the field they are going to protect, followed by the name of the property and a code block that defines the get and set accessors.

                class Time
                {
                  private int seconds;
               
                  public int sec
                  {
                    get { return seconds; }
                    set { seconds = value; }
                  }
                }
              
Mikael Olsson
Chapter 16. Indexers
Abstract
Indexers allow an object to be treated as an array. They are declared in the same way as properties, except that the this keyword is used instead of a name and their accessors take parameters. In the following example, the indexer corresponds to an object array called data, so the type of the indexer is set to object.

                class MyArray
                {
                  object[] data = new object[10];
               
                  public object this[int i]
                  {
                    get { return data[i]; }
                    set { data[i] = value; }
                  }
                }
              
Mikael Olsson
Chapter 17. Interfaces
Abstract
An interface is used to specify members that deriving classes must implement. They are defined with the interface keyword followed by a name and a code block. Their naming convention is to start with a capital I and then to have each word initially capitalized.

                interface IMyInterface {}
              
Mikael Olsson
Chapter 18. Abstract
Abstract
An abstract class provides a partial implementation that other classes can build on. When a class is declared as abstract, it means that the class can contain incomplete members that must be implemented in derived classes, in addition to normal class members.
Mikael Olsson
Chapter 19. Namespaces
Abstract
Namespaces provide a way to group related top-level members into a hierarchy. They are also used to avoid naming conflicts. A top-level member, such as a class, that is not included in a namespace is said to belong to the default namespace. It can be moved to another namespace by being enclosed in a namespace block. The naming convention for namespaces is the same as for classes, with each word initially capitalized.

                namespace MyNamespace
                {
                  class MyClass {}
                }
              
Mikael Olsson
Chapter 20. Enum
Abstract
An enumeration is a special kind of value type consisting of a list of named constants. To create one, you use the enum keyword followed by a name and a code block containing a comma-separated list of constant elements.

                enum State { Run, Wait, Stop };
              
Mikael Olsson
Chapter 21. Exception Handling
Abstract
Exception handling allows programmers to deal with unexpected situations that may occur in programs. As an example, consider opening a file using the StreamReader class in the System.IO namespace. To see what kinds of exceptions this class may throw, you can hover the cursor over the class name in Visual Studio. For instance, you may see the System.IO exceptions FileNotFoundException and DirectoryNotFoundException. If any of those exceptions occurs, the program will terminate with an error message.

                using System;
                using System.IO;
               
                class ErrorHandling
                {
                  static void Main()
                  {
                    // Run-time error
                    StreamReader sr = new StreamReader("missing.txt");
                  }
                }
              
Mikael Olsson
Chapter 22. Operator Overloading
Abstract
Operator overloading allows operators to be redefined and used where one or both of the operands are of a certain class. When done correctly, this can simplify the code and make user-defined types as easy to use as the simple types.
Mikael Olsson
Chapter 23. Custom Conversions
Abstract
This chapter covers how to define custom type conversions for an object. As seen in the following example, a class called MyNum is created with a single int field and a constructor. With a custom type conversion, it becomes possible to allow an int to be implicitly converted to an object of this class.

                class MyNum
                {
                  public int val;
                  public MyNum(int i) { val = i; }
                }
              
Mikael Olsson
Chapter 24. Struct
Abstract
The struct keyword in C# is used to create value types. A struct is similar to a class in that it represents a structure with mainly field and method members. However, a struct is a value type, whereas a class is a reference type. Therefore, a struct variable directly stores the data of the struct, while a class variable only stores a reference to an object allocated in memory.
Mikael Olsson
Chapter 25. Preprocessors
Abstract
C# includes a set of preprocessor directives that are mainly used for conditional compilation. Although the C# compiler does not have a separate preprocessor, as C and C++ compilers, the directives shown here are processed as if there were one. That is, they appear to be processed before the actual compilation takes place.
Mikael Olsson
Chapter 26. Delegates
Abstract
A delegate is a type used to reference a method. This allows methods to be assigned to variables and passed as arguments. The delegate’s declaration specifies the method signature to which objects of the delegate type can refer. Delegates are by convention named with each word initially capitalized, followed by Delegate at the end of the name.

                delegate void MyDelegate(string s);
              
Mikael Olsson
Chapter 27. Events
Abstract
Events enable an object to notify other objects when something of interest occurs. The object that raises the event is called the publisher and the objects that handle the event are called subscribers.
Mikael Olsson
Chapter 28. Generics
Abstract
Generics refer to the use of type parameters, which provide a way to design code templates that can operate with different data types. Specifically, it is possible to create generic methods, classes, interfaces, delegates, and events.
Mikael Olsson
Chapter 29. Constants
Abstract
A variable in C# can be made into a compile-time constant by adding the const keyword before the data type. This modifier means that the variable cannot be changed and it must therefore be assigned a value at the same time as it is declared. Any attempts to assign a new value to the constant will result in a compile-time error.
Mikael Olsson
Chapter 30. Asynchronous Methods
Abstract
An asynchronous method is a method that can return before it has finished executing. Any method that performs a potentially long-running task, such as accessing a web resource or reading a file, can be made asynchronous to improve the responsiveness of the program. This is especially important in graphical applications, because any method that takes a long time to execute on the user interface thread will cause the program to be unresponsive while waiting for that method to complete.
Mikael Olsson
Backmatter
Metadaten
Titel
C# 7 Quick Syntax Reference
verfasst von
Mikael Olsson
Copyright-Jahr
2018
Verlag
Apress
Electronic ISBN
978-1-4842-3817-2
Print ISBN
978-1-4842-3816-5
DOI
https://doi.org/10.1007/978-1-4842-3817-2