Skip to content

Commit 0ef4f40

Browse files
committed
Fix small open issues.
- #33341 already addressed in #36428. - Fixes #37294: Add text that the shortened format is valid only when the runtime type matches the variable type. - Fixes #37295: Don't use `ID` in the sample. - Fixes #37296: Fix nullable warnings. Other issue comments are incorrect. - Fixes #41748: Change the sample so the constructor is relevant. - Fixes #42858: Add an explanation on declaring variables in top level statements.
1 parent ff74034 commit 0ef4f40

File tree

7 files changed

+39
-31
lines changed

7 files changed

+39
-31
lines changed

docs/csharp/fundamentals/coding-style/coding-conventions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: ".NET Coding Conventions"
33
description: Learn about commonly used coding conventions in C#. Coding conventions create a consistent look to the code and facilitate copying, changing, and maintaining the code. This article also includes the docs repo coding guidelines
4-
ms.date: 07/27/2023
4+
ms.date: 01/14/2025
55
helpviewer_keyword:
66
- "coding conventions, C#"
77
- "Visual C#, coding conventions"
@@ -128,7 +128,7 @@ If the divisor is 0, the second clause in the `if` statement would cause a run-t
128128

129129
### `new` operator
130130

131-
- Use one of the concise forms of object instantiation, as shown in the following declarations.
131+
- Use one of the concise forms of object instantiation when the variable type matches the object type, as shown in the following declarations. This form isn't valid when the variable is an interface type, or a base class of the runtime type.
132132

133133
:::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet19":::
134134

@@ -172,7 +172,7 @@ Call [static](../../language-reference/keywords/static.md) members by using the
172172

173173
:::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet26":::
174174

175-
- Rename properties when the property names in the result would be ambiguous. For example, if your query returns a customer name and a distributor ID, instead of leaving them as `Name` and `ID` in the result, rename them to clarify that `Name` is the name of a customer, and `ID` is the ID of a distributor.
175+
- Rename properties when the property names in the result would be ambiguous. For example, if your query returns a customer name and a distributor name, instead of leaving them as a form of `Name` in the result, rename them to clarify `CustomerName` is the name of a customer, and `DistributorName` is the name of a distributor.
176176

177177
:::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet27":::
178178

docs/csharp/fundamentals/coding-style/snippets/coding-conventions/coding-conventions.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFramework>net8.0-windows</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<TargetFramework>net9.0-windows</TargetFramework>
67
<Nullable>enable</Nullable>
78
<ImplicitUsings>enable</ImplicitUsings>
89
<StartupObject>Coding_Conventions_Examples.Program</StartupObject>

docs/csharp/fundamentals/coding-style/snippets/coding-conventions/program.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,41 +214,41 @@ static void Main(string[] args)
214214

215215
ExampleClass.totalInstances = 1;
216216

217-
var customers = new List<Customer>
217+
var Customers = new List<Customer>
218218
{
219219
new Customer { Name = "Jones", ID = 432, City = "Redmond" }
220220
};
221221

222222
// Check shop name to use this.
223-
var distributors = new List<Distributor>
223+
var Distributors = new List<Distributor>
224224
{
225225
new Distributor { Name = "ShopSmart", ID = 11302, City = "Redmond" }
226226
};
227227

228228
//<snippet25>
229229
//<snippet28>
230-
var seattleCustomers = from customer in customers
230+
var seattleCustomers = from customer in Customers
231231
//</snippet28>
232232
where customer.City == "Seattle"
233233
select customer.Name;
234234
//</snippet25>
235235

236236
//<snippet26>
237237
var localDistributors =
238-
from customer in customers
239-
join distributor in distributors on customer.City equals distributor.City
238+
from customer in Customers
239+
join distributor in Distributors on customer.City equals distributor.City
240240
select new { Customer = customer, Distributor = distributor };
241241
//</snippet26>
242242

243243
//<snippet27>
244244
var localDistributors2 =
245-
from customer in customers
246-
join distributor in distributors on customer.City equals distributor.City
247-
select new { CustomerName = customer.Name, DistributorID = distributor.ID };
245+
from customer in Customers
246+
join distributor in Distributors on customer.City equals distributor.City
247+
select new { CustomerName = customer.Name, DistributorName = distributor.Name };
248248
//</snippet27>
249249

250250
//<snippet29>
251-
var seattleCustomers2 = from customer in customers
251+
var seattleCustomers2 = from customer in Customers
252252
where customer.City == "Seattle"
253253
orderby customer.Name
254254
select customer;
@@ -257,13 +257,13 @@ orderby customer.Name
257257
// #30 is in class CompoundFrom
258258

259259
var customerDistributorNames =
260-
from customer in customers
261-
join distributor in distributors on customer.City equals distributor.City
260+
from customer in Customers
261+
join distributor in Distributors on customer.City equals distributor.City
262262
select new { CustomerName = customer.Name, DistributorID = distributor.ID };
263263

264264
var customerDistributorNames2 =
265-
from customer in customers
266-
from distributor in distributors
265+
from customer in Customers
266+
from distributor in Distributors
267267
where customer.City == distributor.City
268268
select new { CustomerName = customer.Name, DistributorID = distributor.ID };
269269

@@ -367,7 +367,7 @@ class CompoundFrom
367367
public class Student
368368
{
369369
public string? LastName { get; set; }
370-
public List<int>? Scores { get; set; }
370+
public ICollection<int> Scores { get; set; } = default!;
371371
}
372372

373373
static void Main()
@@ -377,16 +377,16 @@ static void Main()
377377
// each element in the list contains an inner sequence of scores.
378378
List<Student> students = new List<Student>
379379
{
380-
new Student {LastName="Omelchenko", Scores= new List<int> {97, 72, 81, 60}},
381-
new Student {LastName="O'Donnell", Scores= new List<int> {75, 84, 91, 39}},
382-
new Student {LastName="Mortensen", Scores= new List<int> {88, 94, 65, 85}},
383-
new Student {LastName="Garcia", Scores= new List<int> {97, 89, 85, 82}},
384-
new Student {LastName="Beebe", Scores= new List<int> {35, 72, 91, 70}}
380+
new Student {LastName="Omelchenko", Scores = [97, 72, 81, 60]},
381+
new Student {LastName="O'Donnell", Scores = [75, 84, 91, 39]},
382+
new Student {LastName="Mortensen", Scores = [88, 94, 65, 85]},
383+
new Student {LastName="Garcia", Scores = [97, 89, 85, 82]},
384+
new Student {LastName="Beebe", Scores = [35, 72, 91, 70]}
385385
};
386386

387387
//<snippet30>
388388
var scoreQuery = from student in students
389-
from score in student.Scores!
389+
from score in student.Scores
390390
where score > 90
391391
select new { Last = student.LastName, score };
392392
//</snippet30>

docs/csharp/programming-guide/classes-and-structs/snippets/using-constructors/Program.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
//<Snippet1>
22
public class Taxi
33
{
4-
public bool IsInitialized;
4+
private string taxiTag;
55

6-
public Taxi()
6+
public Taxi(string tag)
77
{
8-
IsInitialized = true;
8+
taxiTag = tag;
9+
}
10+
11+
public override string ToString()
12+
{
13+
return $"Taxi: {taxiTag}";;
914
}
1015
}
1116

1217
class TestTaxi
1318
{
1419
static void Main()
1520
{
16-
Taxi t = new Taxi();
17-
Console.WriteLine(t.IsInitialized);
21+
Taxi t = new Taxi("Tag1345");
22+
Console.WriteLine(t);
1823
}
1924
}
2025
//</Snippet1>

docs/csharp/programming-guide/classes-and-structs/snippets/using-constructors/UsingConstructors.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<RootNamespace>UsingConstructors</RootNamespace>

docs/csharp/programming-guide/classes-and-structs/using-constructors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Using Constructors"
33
description: This example shows how a class is instantiated by using the new operator in C#. The simple constructor is invoked after memory is allocated for the new object.
4-
ms.date: 11/22/2024
4+
ms.date: 01/14/2025
55
helpviewer_keywords:
66
- "constructors [C#], about constructors"
77
---

docs/csharp/tutorials/top-level-statements.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ Should I use top level statements in all my programs?
100100
Better not tell you now.
101101
```
102102

103+
The code to generate an answer includes a variable declaration in your top level statements. The compiler includes that declaration in the compiler generated `Main` method. Because these variable declarations are local variables, you can't include the `static` modifier.
104+
103105
This code answers the questions, but let's add one more feature. You'd like your question app to simulate thinking about the answer. You can do that by adding a bit of ASCII animation, and pausing while working. Add the following code after the line that echoes the question:
104106

105107
:::code language="csharp" source="snippets/top-level-statements/UtilitiesPassOne.cs" ID="AnimationFirstPass":::

0 commit comments

Comments
 (0)