Why Guid.TryParse allows trailing/leading spaces

Advertisements One of our existing codes failed because a developer used Guid.TryParse to check if an input string is a valid Guid value and the input itself has a trailing space. if (Guid.TryParseExact("0c652e2a-ef02-4eff-8855-78122d8b6c0b ", "D", out var guid1)) _logger.LogWarning("guid1"); if (Guid.TryParse("0c652e2a-ef02-4eff-8855-78122d8b6c0b ", out var guid2)) _logger.LogWarning("guid2"); if (Guid.TryParse(" 0c652e2a-ef02-4eff-8855-78122d8b6c0b ", out var guid3)) _logger.LogWarning("guid3"); if… Read More Why Guid.TryParse allows trailing/leading spaces

ASP.NET core web api db seeder The seed entity for entity type 'X' cannot be added because it has the navigation 'something' set

Advertisements I’m learning ASP.NET 6.0 Core Web api, I have a car model that looks like this public class Car { public int id { get; set; } public string make { get; set; } public string model { get; set; } public int price { get; set; } public string rentalType { get; set;… Read More ASP.NET core web api db seeder The seed entity for entity type 'X' cannot be added because it has the navigation 'something' set

I have a list of ids and I would like to build an if statement from each of those ids or if there is a better way?

Advertisements I have one list that contains just ids var listVal = new List<int>() { 1,2,3,4 }; another foreach if (page.Id == 1 || page.Id == 2 || page.Id == 3 || page.Id == 4 ) Looking for a way to dynamically generate that if statement from the list listVal above or a better way… Read More I have a list of ids and I would like to build an if statement from each of those ids or if there is a better way?

Creating objects dynamically

Advertisements In a UI app I’ve build, I create a label like this: private void InitializeCurrentPerlLabel() { currentPerlLabel = new System.Windows.Forms.Label(); SuspendLayout(); currentPerlLabel.AutoSize = true; currentPerlLabel.Location = new System.Drawing.Point(10, 10); currentPerlLabel.Name = "currentPerlLabel"; currentPerlLabel.Size = new System.Drawing.Size(35, 35); currentPerlLabel.TabIndex = 0; currentPerlLabel.Font = new Font(Font, FontStyle.Bold); Controls.Add(currentPerlLabel); Name = "BBUI"; ResumeLayout(false); PerformLayout(); } …and I… Read More Creating objects dynamically

FileStream cannot be closed when an exception is occurred in runtime

Advertisements The following is a part of my code: try { using (ExcelEngine excelEngine = new ExcelEngine()) { . . . var path = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Data\Temp", userId + ".xlsx"); //Load the existing Excel workbook into IWorkbook FileStream inputStream = new FileStream(path, FileMode.Open); . . . //***For example, an exception is occurred here in runtime*** .… Read More FileStream cannot be closed when an exception is occurred in runtime

How to fix "Target runtime doesn't support covariant types in overrides?

Advertisements I have Builder, let’s see code: public abstract class UISchemaBuilderBase { protected readonly UISchemaSpecification UiSchemaSpecification; public UISchemaSpecification Builder() => UiSchemaSpecification; public virtual UISchemaBuilderBase AddBoolean( string fieldName, string groupName = null) { .. some logic UiSchemaSpecification.Properties[ fieldName ] = schema; return this; } } public class UISchemaBuilder : UISchemaBuilderBase { public UISchemaBuilder() : base(new UISchemaSpecification())… Read More How to fix "Target runtime doesn't support covariant types in overrides?

passing object references by value in C#

Advertisements var test = new TestClass(); SomeMethod(test); Console.WriteLine(test); SomeMethod2(test); Console.WriteLine(test); Console.ReadKey(); void SomeMethod(TestClass a) { a = null; } void SomeMethod2(TestClass a) { a.intThing = 1; } public class TestClass { public int intThing =0; public override string ToString() { return $"{intThing}"; } } A bit confused on the output of this here – SomeMethod… Read More passing object references by value in C#

When editing an array "in place", why does overwriting the array not work, but overwritting each element does?

Advertisements In C#, I have a function that should edit an array (not return new one). Why when I try to set the input variable to a new/update array it doesn’t work, but when I loop over the array and change all the values it does? I’ve been developing for some time and this has… Read More When editing an array "in place", why does overwriting the array not work, but overwritting each element does?