Skip to content

Commit b4b198b

Browse files
committed
Implemented implicit types for new expressions
With C# 9.0, the `new` keyword only needs a type declaration if the type is otherwise unknown (e.g., `var`) or ambiguous (e.g., with an interface, or overloaded method). Otherwise, it can just use `new()`. This is especially useful for property initializers, which can be incredibly repetitive. Note: This apparently cannot be done for generics (e.g., `new T()` cannot become `new()`). I'll need to look into that, but for now it's useful to know that it doesn't work.
1 parent 1bafc66 commit b4b198b

50 files changed

Lines changed: 83 additions & 86 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

OnTopic.AspNetCore.Mvc.Tests/OnTopic.AspNetCore.Mvc.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netcoreapp3.0</TargetFramework>
55
<IsPackable>false</IsPackable>
6+
<LangVersion>9.0</LangVersion>
67
</PropertyGroup>
78

89
<ItemGroup>

OnTopic.AspNetCore.Mvc.Tests/TopicControllerTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public TopicControllerTest() {
7373
RouteData = routes,
7474
ActionDescriptor = new ControllerActionDescriptor()
7575
};
76-
_context = new ControllerContext(actionContext);
76+
_context = new(actionContext);
7777

7878
}
7979

@@ -131,11 +131,11 @@ public void SitemapController_Index_ReturnsSitemapXml() {
131131

132132
var actionContext = new ActionContext {
133133
HttpContext = new DefaultHttpContext(),
134-
RouteData = new RouteData(),
134+
RouteData = new(),
135135
ActionDescriptor = new ControllerActionDescriptor()
136136
};
137137
var controller = new SitemapController(_topicRepository) {
138-
ControllerContext = new ControllerContext(actionContext)
138+
ControllerContext = new(actionContext)
139139
};
140140
var result = controller.Index() as ContentResult;
141141
var model = result.Content as string;

OnTopic.AspNetCore.Mvc.Tests/ValidateTopicAttributeTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static ActionExecutingContext GetActionExecutingContext(Controller contro
4040

4141
var actionContext = new ActionContext(
4242
new DefaultHttpContext(),
43-
new RouteData(),
43+
new(),
4444
new ControllerActionDescriptor(),
4545
modelState
4646
);
@@ -63,10 +63,10 @@ public static ActionExecutingContext GetActionExecutingContext(Controller contro
6363
/// Generates a barebones <see cref="ControllerContext"/> for testing a controller.
6464
/// </summary>
6565
public static ControllerContext GetControllerContext() =>
66-
new ControllerContext(
67-
new ActionContext() {
66+
new(
67+
new() {
6868
HttpContext = new DefaultHttpContext(),
69-
RouteData = new RouteData(),
69+
RouteData = new(),
7070
ActionDescriptor = new ControllerActionDescriptor()
7171
}
7272
);
@@ -78,7 +78,7 @@ public static ControllerContext GetControllerContext() =>
7878
/// Generates a barebones <see cref="ControllerContext"/> for testing a controller.
7979
/// </summary>
8080
public static TopicController GetTopicController(Topic topic) =>
81-
new TopicController(
81+
new(
8282
new DummyTopicRepository(),
8383
new DummyTopicMappingService()
8484
) {

OnTopic.AspNetCore.Mvc/Controllers/SitemapController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public virtual ActionResult Index(bool indent = false, bool includeMetadata = fa
146146
/// <returns>A Sitemap.org sitemap.</returns>
147147
[Obsolete("The GenerateSitemap() method should not be public. It will be marked private in OnTopic Library 5.0.")]
148148
public virtual XDocument GenerateSitemap(Topic rootTopic, bool includeMetadata = false) =>
149-
new XDocument(
149+
new(
150150
new XElement(_sitemapNamespace + "urlset",
151151
from topic in rootTopic?.Children
152152
select AddTopic(topic, includeMetadata)

OnTopic.AspNetCore.Mvc/Controllers/TopicController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public async virtual Task<IActionResult> IndexAsync(string path) {
122122
/// <returns>The created <see cref="TopicViewResult"/> object for the response.</returns>
123123
[NonAction]
124124
public virtual TopicViewResult TopicView(object model, string? viewName = null) =>
125-
new TopicViewResult(ViewData, TempData, model, CurrentTopic?.ContentType, viewName);
125+
new(ViewData, TempData, model, CurrentTopic?.ContentType, viewName);
126126

127127
} //Class
128128
} //Namespace

OnTopic.Data.Sql/SqlTopicRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
269269
| Attempt to resolve outstanding relationships
270270
\-----------------------------------------------------------------------------------------------------------------------*/
271271
foreach (var unresolvedTopic in unresolvedTopics) {
272-
Save(unresolvedTopic, false, isDraft, connection, new List<Topic>(), version);
272+
Save(unresolvedTopic, false, isDraft, connection, new(), version);
273273
}
274274

275275
/*------------------------------------------------------------------------------------------------------------------------

OnTopic.Tests/AttributeValueCollectionTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public void Add_ValidAttributeValue_IsReturned() {
453453
var topic = TopicFactory.Create("Test", "Container");
454454

455455
topic.Attributes.Remove("Key");
456-
topic.Attributes.Add(new AttributeValue("Key", "NewKey", false));
456+
topic.Attributes.Add(new("Key", "NewKey", false));
457457

458458
Assert.AreEqual<string>("NewKey", topic.Key);
459459

@@ -473,7 +473,7 @@ public void Add_ValidAttributeValue_IsReturned() {
473473
public void Add_InvalidAttributeValue_ThrowsException() {
474474
var topic = TopicFactory.Create("Test", "Container");
475475
topic.Attributes.Remove("Key");
476-
topic.Attributes.Add(new AttributeValue("Key", "# ?"));
476+
topic.Attributes.Add(new("Key", "# ?"));
477477
}
478478

479479
/*==========================================================================================================================

OnTopic.Tests/BindingModels/ContentTypeDescriptorTopicBindingModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public class ContentTypeDescriptorTopicBindingModel : BasicTopicBindingModel {
2121

2222
public ContentTypeDescriptorTopicBindingModel(string? key = null) : base(key, "ContentTypeDescriptor") { }
2323

24-
public List<RelatedTopicBindingModel> ContentTypes { get; } = new List<RelatedTopicBindingModel>();
24+
public List<RelatedTopicBindingModel> ContentTypes { get; } = new();
2525

26-
public List<AttributeDescriptorTopicBindingModel> Attributes { get; } = new List<AttributeDescriptorTopicBindingModel>();
26+
public List<AttributeDescriptorTopicBindingModel> Attributes { get; } = new();
2727

2828
} //Class
2929
} //Namespace

OnTopic.Tests/BindingModels/InvalidChildrenTopicBindingModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class InvalidChildrenTopicBindingModel : BasicTopicBindingModel {
2222

2323
public InvalidChildrenTopicBindingModel(string? key = null) : base(key, "Page") { }
2424

25-
public List<BasicTopicBindingModel> Children { get; } = new List<BasicTopicBindingModel>();
25+
public List<BasicTopicBindingModel> Children { get; } = new();
2626

2727
} //Class
2828
} //Namespace

OnTopic.Tests/BindingModels/InvalidReferenceNameTopicBindingModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class InvalidReferenceNameTopicBindingModel : BasicTopicBindingModel {
2222

2323
public InvalidReferenceNameTopicBindingModel(string? key = null) : base(key, "Page") { }
2424

25-
public RelatedTopicBindingModel TopicReference { get; } = new RelatedTopicBindingModel();
25+
public RelatedTopicBindingModel TopicReference { get; } = new();
2626

2727
} //Class
2828
} //Namespace

0 commit comments

Comments
 (0)