Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Fundamentals section. #23464

Merged
merged 60 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
077956d
Update navigation
BillWagner Mar 23, 2021
a46655f
Move files
BillWagner Mar 24, 2021
28e7194
fix warnings in the fundamentals area
BillWagner Mar 24, 2021
c00b425
update ms-technology metadata.
BillWagner Mar 30, 2021
2444296
Proposed fundamentals TOC
BillWagner May 6, 2021
8d5ddfd
fix a bad merge
BillWagner May 6, 2021
b19ab3f
update see also section
BillWagner May 6, 2021
e47a5ea
One more set of TOC moves
BillWagner May 7, 2021
e5be4b5
Move program structure section
BillWagner May 7, 2021
f0105d1
clean warnings in structure section
BillWagner May 7, 2021
7dad562
warnings take 2
BillWagner May 7, 2021
a229ba2
move oo section files.
BillWagner May 7, 2021
b09e877
fix snippet build warnings.
BillWagner May 10, 2021
f4a6a37
move articles to functional section
BillWagner May 10, 2021
edad1b1
Fix a bad snippet ID conversion.
BillWagner May 11, 2021
0136fe5
Wrong snippet.
BillWagner May 11, 2021
0c41de1
Move fundamental exception content
BillWagner May 11, 2021
871f7e6
more moving exceptions
BillWagner May 11, 2021
84f4a25
move coding style articles
BillWagner May 11, 2021
8c77bcf
fix warnings.
BillWagner May 11, 2021
08140b5
build warnings
BillWagner May 11, 2021
0b6fe09
edit pass
BillWagner May 12, 2021
1e637f7
proofread.
BillWagner May 13, 2021
08aac27
continue editing types section
BillWagner May 13, 2021
20beb67
edit generics
BillWagner May 13, 2021
4a14c60
edit for anonymous types
BillWagner May 13, 2021
42497aa
fix build issues.
BillWagner May 13, 2021
343e075
edit program structure
BillWagner May 13, 2021
c743780
build error
BillWagner May 13, 2021
3b6158e
no interactive top level statements
BillWagner May 13, 2021
93d1715
update H1
BillWagner May 13, 2021
2557706
review samples in OO section
BillWagner May 14, 2021
0c863ae
finish oo tutorials
BillWagner May 14, 2021
f9a2d2a
fix issues filed on these articles
BillWagner May 14, 2021
3d422af
funtional is in place.
BillWagner May 14, 2021
70d8724
light edit on exceptions section
BillWagner May 14, 2021
3b90c34
fix warnings
BillWagner May 14, 2021
e4f9a72
finish moving coding conventions
BillWagner May 14, 2021
27b3e05
fix warnings
BillWagner May 14, 2021
f1467a3
don't use interactive on C# 9 feature
BillWagner May 14, 2021
69ead81
clean up
BillWagner May 17, 2021
02652d3
replace redirected links
BillWagner May 17, 2021
22b939c
replace relative links
BillWagner May 17, 2021
7ce5d96
fix warnings with repeated H2s
BillWagner May 17, 2021
16981ff
Apply suggestions from code review
BillWagner May 17, 2021
143331e
respond to feedback
BillWagner May 17, 2021
7010f45
fix broken links.
BillWagner May 17, 2021
c3f53fd
respond to feedback.
BillWagner May 17, 2021
b139af3
fix warnings
BillWagner May 17, 2021
3c66c9d
move fundamentals tutorials up one level.
BillWagner May 17, 2021
8a307fe
fix warnings
BillWagner May 17, 2021
f2fcfb9
Apply suggestions from code review
BillWagner May 18, 2021
3772439
Update docs/csharp/fundamentals/types/index.md
BillWagner May 18, 2021
231564d
Update docs/csharp/fundamentals/types/interfaces.md
BillWagner May 18, 2021
5387151
Apply suggestions from code review
BillWagner May 18, 2021
49b6b91
Apply suggestions from code review
BillWagner May 18, 2021
36ae584
respond to feedback.
BillWagner May 18, 2021
fc63544
missed a bad merge
BillWagner May 18, 2021
8ee0579
move remaining samples.
BillWagner May 18, 2021
cf26e6d
fix misspelling
BillWagner May 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Tom Dykstra <[email protected]>
Co-authored-by: Genevieve Warren <[email protected]>
  • Loading branch information
3 people committed May 18, 2021
commit 16981fff1b34c7804bac32c2dc0ed41c14dea612
4 changes: 2 additions & 2 deletions docs/csharp/fundamentals/program-structure/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ms.assetid: 5ae964a5-0ef0-40fe-88fb-6d1793371d0d
---
# General Structure of a C# Program

C# programs consists of one or more files. Each file contains zero or more namespaces. A namespace contains types such as classes, structs, interfaces, enumerations, and delegates, or other namespaces. The following example is the skeleton of a C# program that contains all of these elements.
C# programs consist of one or more files. Each file contains zero or more namespaces. A namespace contains types such as classes, structs, interfaces, enumerations, and delegates, or other namespaces. The following example is the skeleton of a C# program that contains all of these elements.

:::code language="csharp" source="snippets/toplevel-structure/Program.cs":::

Expand All @@ -18,7 +18,7 @@ The preceding example uses *top-level statements* for the program's entry point.

## Related Sections

You learned about these program elements in the [types](../types/index.md) section of the fundamentals guide:
You learn about these program elements in the [types](../types/index.md) section of the fundamentals guide:

- [Classes](../types/classes.md)
- [Structs](../../language-reference/builtin-types/struct.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ For information about how to write application code with an implicit entry point
- `Main` is declared inside a class or struct. `Main` must be [`static`](../../language-reference/keywords/static.md) and it need not be [`public`](../../language-reference/keywords/public.md). (In the earlier example, it receives the default access of [`private`](../../language-reference/keywords/private.md).) The enclosing class or struct is not required to be static.
>>>>>>> cd631f0b85 (edit program structure)
- `Main` can either have a `void`, `int`, or, starting with C# 7.1, `Task`, or `Task<int>` return type.
- If and only if `Main` returns a `Task` or `Task<int>`, the declaration of `Main` may include the [`async`](../../language-reference/keywords/async.md) modifier. Note that this specifically excludes an `async void Main` method.
- If and only if `Main` returns a `Task` or `Task<int>`, the declaration of `Main` may include the [`async`](../../language-reference/keywords/async.md) modifier. This specifically excludes an `async void Main` method.
- The `Main` method can be declared with or without a `string[]` parameter that contains command-line arguments. When using Visual Studio to create Windows applications, you can add the parameter manually or else use the <xref:System.Environment.GetCommandLineArgs> method to obtain the command-line arguments. Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument in the `args` array, but it is the first element of the <xref:System.Environment.GetCommandLineArgs> method.

The following list shows valid `Main` signatures:
Expand All @@ -51,7 +51,7 @@ public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }
```

The preceding examples all use the `public` accessor modifier. That is typical, but not required.
The preceding examples all use the `public` accessor modifier. That's typical, but not required.

The addition of `async` and `Task`, `Task<int>` return types simplifies program code when console applications need to start and `await` asynchronous operations in `Main`.

Expand Down Expand Up @@ -107,14 +107,14 @@ if ($LastExitCode -eq 0) {
Write-Host "Return value = " $LastExitCode
```

```txt
```output
Execution succeeded
Return value = 0
```

### Async Main return values

Async Main return values move the boilerplate code necessary for calling asynchronous methods in `Main` to code generated by the compiler. Previously, you would need to write this construct to call asynchronous code and ensure your program ran until the asynchronous operation completed:
When you declare an `async` return value for `Main`, the compiler generates the boilerplate code for calling asynchronous methods in `Main`. If you don't specify the `async` keyword, you need to write that code yourself, as shown in the following example. The code in the example ensures that your program runs until the asynchronous operation is completed:

```csharp
public static void Main()
Expand All @@ -129,11 +129,11 @@ private static async Task<int> AsyncConsoleWork()
}
```

Now, this can be replaced by:
This boilerplate code can be replaced by:

:::code language="csharp" source="snippets/main-arguments/Program.cs":::
:::code language="csharp" source="snippets/main-arguments/Program.cs" id="AsyncMain":::

The advantage of the new syntax is that the compiler always generates the correct code.
An advantage of declaring `Main` as `async` is that the compiler always generates the correct code.

When the application entry point returns a `Task` or `Task<int>`, the compiler generates a new entry point that calls the entry point method declared in the application code. Assuming that this entry point is called `$GeneratedMain`, the compiler generates the following code for these entry points:

Expand Down Expand Up @@ -166,14 +166,14 @@ If the arguments are not used, you can omit `args` from the method signature for
| Return value, uses `await` | `static async Task<int> Main()` |

> [!NOTE]
> To enable command-line arguments in the `Main` method in a Windows Forms application, you must manually modify the signature of `Main` in *program.cs*. The code generated by the Windows Forms designer creates a `Main` without an input parameter. You can also use <xref:System.Environment.CommandLine%2A?displayProperty=nameWithType> or <xref:System.Environment.GetCommandLineArgs%2A?displayProperty=nameWithType> to access the command-line arguments from any point in a console or Windows application.
> You can also use <xref:System.Environment.CommandLine%2A?displayProperty=nameWithType> or <xref:System.Environment.GetCommandLineArgs%2A?displayProperty=nameWithType> to access the command-line arguments from any point in a console or Windows Forms application. To enable command-line arguments in the `Main` method signature in a Windows Forms application, you must manually modify the signature of `Main`. The code generated by the Windows Forms designer creates `Main` without an input parameter.

The parameter of the `Main` method is a <xref:System.String> array that represents the command-line arguments. Usually you determine whether arguments exist by testing the `Length` property, for example:

:::code language="csharp" source="snippets/main-command-line/Program.cs" ID="Snippet4":::

> [!TIP]
> The `args` array cannot be null. So, it's safe to access the `Length` property without null checking.
> The `args` array can't be null. So, it's safe to access the `Length` property without null checking.

You can also convert the string arguments to numeric types by using the <xref:System.Convert> class or the `Parse` method. For example, the following statement converts the `string` to a `long` number by using the <xref:System.Int64.Parse%2A> method:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ helpviewer_keywords:
- "C# language, top-level statements"
- "C# language, Main method"
---
# Top-level statements - programs without Main methods
# Top-level statements - programs without `Main` methods

Starting in C# 9, you don't have to explicitly include a `Main` method in a console application project. Instead, you can use the *top-level statements* feature to minimize the code you have to write. In this case, the compiler generates a class and `Main` method entry point for the application.
BillWagner marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/fundamentals/types/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ It can be inconvenient to create a named type for simple sets of related values

Ordinary value types can't have a value of [`null`](../../language-reference/keywords/null.md). However, you can create *nullable value types* by appending a `?` after the type. For example, `int?` is an `int` type that can also have the value [`null`](../../language-reference/keywords/null.md). Nullable value types are instances of the generic struct type <xref:System.Nullable%601?displayProperty=nameWithType>. Nullable value types are especially useful when you're passing data to and from databases in which numeric values might be `null`. For more information, see [Nullable value types](../../language-reference/builtin-types/nullable-value-types.md).

## Compile-time type and runtime type
## Compile-time type and run-time type

A variable can have different compile-time and run-time types. The *compile-time type* is the declared or inferred type of the variable in the source code. The *run-time type* is the type of the instance referred to by that variable. Often those two types are the same, as in the following example:

Expand Down