diff --git a/src/EFCore.Design/Design/Internal/CSharpHelper.cs b/src/EFCore.Design/Design/Internal/CSharpHelper.cs index fa17f6488bf..c005f957eb4 100644 --- a/src/EFCore.Design/Design/Internal/CSharpHelper.cs +++ b/src/EFCore.Design/Design/Internal/CSharpHelper.cs @@ -233,7 +233,7 @@ public virtual string Identifier(string name, ICollection? scope = null, if (partStart != name.Length) { - builder.Append(name.Substring(partStart)); + builder.Append(name[partStart..]); } if (builder.Length == 0 diff --git a/src/EFCore.Design/Design/Internal/DatabaseOperations.cs b/src/EFCore.Design/Design/Internal/DatabaseOperations.cs index f8fe93b3c0c..38967a4d18d 100644 --- a/src/EFCore.Design/Design/Internal/DatabaseOperations.cs +++ b/src/EFCore.Design/Design/Internal/DatabaseOperations.cs @@ -127,7 +127,7 @@ public virtual SavedModelFiles ScaffoldContext( return null; } - var subPath = outputDir.Substring(projectDir.Length); + var subPath = outputDir[projectDir.Length..]; return !string.IsNullOrWhiteSpace(subPath) ? string.Join( @@ -151,7 +151,7 @@ private static string NormalizeDir(string path) return path; } - var last = path[path.Length - 1]; + var last = path[^1]; return last == Path.DirectorySeparatorChar || last == Path.AltDirectorySeparatorChar ? path diff --git a/src/EFCore.Design/Design/Internal/DbContextOperations.cs b/src/EFCore.Design/Design/Internal/DbContextOperations.cs index 0321e600f62..aaa3eb71b2f 100644 --- a/src/EFCore.Design/Design/Internal/DbContextOperations.cs +++ b/src/EFCore.Design/Design/Internal/DbContextOperations.cs @@ -191,7 +191,7 @@ public virtual void Optimize(string? outputDir, string? modelNamespace, string? return null; } - var subPath = outputDir.Substring(projectDir.Length); + var subPath = outputDir[projectDir.Length..]; return !string.IsNullOrWhiteSpace(subPath) ? string.Join( diff --git a/src/EFCore.Design/Design/Internal/MigrationsOperations.cs b/src/EFCore.Design/Design/Internal/MigrationsOperations.cs index 43d278e9319..158acbb68d7 100644 --- a/src/EFCore.Design/Design/Internal/MigrationsOperations.cs +++ b/src/EFCore.Design/Design/Internal/MigrationsOperations.cs @@ -110,7 +110,7 @@ public virtual MigrationFiles AddMigration( return null; } - var subPath = outputDir.Substring(_projectDir.Length); + var subPath = outputDir[_projectDir.Length..]; return !string.IsNullOrWhiteSpace(subPath) ? string.Join( diff --git a/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs b/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs index ac84071afec..3bc10eeabcf 100644 --- a/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs +++ b/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs @@ -100,7 +100,7 @@ public virtual ScaffoldedMigration ScaffoldMigration( var genericMarkIndex = sanitizedContextName.IndexOf('`'); if (genericMarkIndex != -1) { - sanitizedContextName = sanitizedContextName.Substring(0, genericMarkIndex); + sanitizedContextName = sanitizedContextName[..genericMarkIndex]; } if (ContainsForeignMigrations(migrationNamespace!)) @@ -207,7 +207,7 @@ protected virtual string GetSubNamespace(string? rootNamespace, string @namespac return @namespace == rootNamespace ? string.Empty : @namespace.StartsWith(rootNamespace + '.', StringComparison.Ordinal) - ? @namespace.Substring(rootNamespace.Length + 1) + ? @namespace[(rootNamespace.Length + 1)..] : @namespace; } @@ -252,7 +252,7 @@ public virtual MigrationFiles RemoveMigration( .ToList(); if (migrations.Count != 0) { - var migration = migrations[migrations.Count - 1]; + var migration = migrations[^1]; model = Dependencies.SnapshotModelProcessor.Process(migration.TargetModel); if (!Dependencies.MigrationsModelDiffer.HasDifferences( @@ -277,7 +277,7 @@ public virtual MigrationFiles RemoveMigration( { Dependencies.Migrator.Migrate( migrations.Count > 1 - ? migrations[migrations.Count - 2].GetId() + ? migrations[^2].GetId() : Migration.InitialDatabase); } else @@ -314,7 +314,7 @@ public virtual MigrationFiles RemoveMigration( } model = migrations.Count > 1 - ? Dependencies.SnapshotModelProcessor.Process(migrations[migrations.Count - 2].TargetModel) + ? Dependencies.SnapshotModelProcessor.Process(migrations[^2].TargetModel) : null; } else diff --git a/src/EFCore.Design/Migrations/Internal/SnapshotModelProcessor.cs b/src/EFCore.Design/Migrations/Internal/SnapshotModelProcessor.cs index 82dc8f3277c..2999982e42d 100644 --- a/src/EFCore.Design/Migrations/Internal/SnapshotModelProcessor.cs +++ b/src/EFCore.Design/Migrations/Internal/SnapshotModelProcessor.cs @@ -34,7 +34,7 @@ public SnapshotModelProcessor( typeof(RelationalAnnotationNames) .GetRuntimeFields() .Where(p => p.Name != nameof(RelationalAnnotationNames.Prefix)) - .Select(p => ((string)p.GetValue(null)!).Substring(RelationalAnnotationNames.Prefix.Length - 1))); + .Select(p => ((string)p.GetValue(null)!)[(RelationalAnnotationNames.Prefix.Length - 1)..])); _modelRuntimeInitializer = modelRuntimeInitializer; } @@ -107,7 +107,7 @@ private void ProcessElement(IReadOnlyAnnotatable? metadata, string version) var colon = annotation.Name.IndexOf(':'); if (colon > 0) { - var stripped = annotation.Name.Substring(colon); + var stripped = annotation.Name[colon..]; if (_relationalNames.Contains(stripped)) { mutableMetadata.RemoveAnnotation(annotation.Name); @@ -121,7 +121,7 @@ private void ProcessElement(IReadOnlyAnnotatable? metadata, string version) else if (!Equals(duplicate.Value, annotation.Value)) { _operationReporter.WriteWarning( - DesignStrings.MultipleAnnotationConflict(stripped.Substring(1))); + DesignStrings.MultipleAnnotationConflict(stripped[1..])); } } } diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpUtilities.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpUtilities.cs index a713a81ca1b..3f4e7e53730 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CSharpUtilities.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CSharpUtilities.cs @@ -136,7 +136,7 @@ public virtual string GenerateCSharpIdentifier( { var proposedIdentifier = identifier.Length > 1 && identifier[0] == '@' - ? "@" + _invalidCharsRegex.Replace(identifier.Substring(1), "_") + ? "@" + _invalidCharsRegex.Replace(identifier[1..], "_") : _invalidCharsRegex.Replace(identifier, "_"); if (string.IsNullOrEmpty(proposedIdentifier)) { diff --git a/src/EFCore.Design/Scaffolding/Internal/CandidateNamingService.cs b/src/EFCore.Design/Scaffolding/Internal/CandidateNamingService.cs index 338d92c4f8e..cf295487a9b 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CandidateNamingService.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CandidateNamingService.cs @@ -128,14 +128,14 @@ private static string FindCommonPrefix(string firstName, IEnumerable pro if (s.Length <= prefixLength || s[prefixLength] != c) { - return firstName.Substring(0, prefixLength); + return firstName[..prefixLength]; } } prefixLength++; } - return firstName.Substring(0, prefixLength); + return firstName[..prefixLength]; } private static string StripId(string commonPrefix) @@ -156,7 +156,7 @@ private static string StripId(string commonPrefix) } return i != 0 - ? commonPrefix.Substring(0, i + 1) + ? commonPrefix[..(i + 1)] : commonPrefix; } } diff --git a/src/EFCore.Proxies/Proxies/Internal/LazyLoadingInterceptor.cs b/src/EFCore.Proxies/Proxies/Internal/LazyLoadingInterceptor.cs index 250d79a7405..1382ff6a045 100644 --- a/src/EFCore.Proxies/Proxies/Internal/LazyLoadingInterceptor.cs +++ b/src/EFCore.Proxies/Proxies/Internal/LazyLoadingInterceptor.cs @@ -60,7 +60,7 @@ public virtual void Intercept(IInvocation invocation) if (_loader != null && methodName.StartsWith("get_", StringComparison.Ordinal)) { - var navigationName = methodName.Substring(4); + var navigationName = methodName[4..]; var navigationBase = _entityType.FindNavigation(navigationName) ?? (INavigationBase?)_entityType.FindSkipNavigation(navigationName); diff --git a/src/EFCore.Proxies/Proxies/Internal/PropertyChangeInterceptorBase.cs b/src/EFCore.Proxies/Proxies/Internal/PropertyChangeInterceptorBase.cs index d1a98ffaddd..404c7c53d13 100644 --- a/src/EFCore.Proxies/Proxies/Internal/PropertyChangeInterceptorBase.cs +++ b/src/EFCore.Proxies/Proxies/Internal/PropertyChangeInterceptorBase.cs @@ -64,6 +64,6 @@ protected virtual string FindPropertyName(IInvocation invocation) } } - return methodName.Substring(4); + return methodName[4..]; } } diff --git a/src/EFCore.Relational/Migrations/Internal/MigrationsIdGenerator.cs b/src/EFCore.Relational/Migrations/Internal/MigrationsIdGenerator.cs index 451704b8219..53e27231928 100644 --- a/src/EFCore.Relational/Migrations/Internal/MigrationsIdGenerator.cs +++ b/src/EFCore.Relational/Migrations/Internal/MigrationsIdGenerator.cs @@ -26,7 +26,7 @@ public class MigrationsIdGenerator : IMigrationsIdGenerator /// doing so can result in application failures when updating to a new Entity Framework Core release. /// public virtual string GetName(string id) - => id.Substring(Format.Length + 1); + => id[(Format.Length + 1)..]; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to diff --git a/src/EFCore.Relational/Query/SqlExpressions/TableExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/TableExpression.cs index 3b871e293ae..948b4c3b31e 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/TableExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/TableExpression.cs @@ -16,7 +16,7 @@ namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions; public sealed class TableExpression : TableExpressionBase, IClonableTableExpressionBase { internal TableExpression(ITableBase table) - : base(table.Name.Substring(0, 1).ToLowerInvariant()) + : base(table.Name[..1].ToLowerInvariant()) { Name = table.Name; Schema = table.Schema; diff --git a/src/EFCore.Relational/Query/SqlExpressions/TableValuedFunctionExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/TableValuedFunctionExpression.cs index 1fa8fa5457d..5dcc1e8cc19 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/TableValuedFunctionExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/TableValuedFunctionExpression.cs @@ -23,7 +23,7 @@ public class TableValuedFunctionExpression : TableExpressionBase /// The arguments of the function. public TableValuedFunctionExpression(IStoreFunction storeFunction, IReadOnlyList arguments) : this( - storeFunction.Name.Substring(0, 1).ToLowerInvariant(), + storeFunction.Name[..1].ToLowerInvariant(), storeFunction, arguments) { diff --git a/src/EFCore.Relational/Storage/Internal/NamedConnectionStringResolverBase.cs b/src/EFCore.Relational/Storage/Internal/NamedConnectionStringResolverBase.cs index d2d8adeac74..14d12a38457 100644 --- a/src/EFCore.Relational/Storage/Internal/NamedConnectionStringResolverBase.cs +++ b/src/EFCore.Relational/Storage/Internal/NamedConnectionStringResolverBase.cs @@ -66,9 +66,9 @@ public virtual string ResolveConnectionString(string connectionString) return null; } - return connectionString.Substring(0, firstEquals).Trim().Equals( + return connectionString[..firstEquals].Trim().Equals( "name", StringComparison.OrdinalIgnoreCase) - ? connectionString.Substring(firstEquals + 1).Trim() + ? connectionString[(firstEquals + 1)..].Trim() : null; } } diff --git a/src/EFCore.Relational/Storage/RelationalTypeMapping.cs b/src/EFCore.Relational/Storage/RelationalTypeMapping.cs index 6641d719821..51936ec0f07 100644 --- a/src/EFCore.Relational/Storage/RelationalTypeMapping.cs +++ b/src/EFCore.Relational/Storage/RelationalTypeMapping.cs @@ -319,7 +319,7 @@ private static string GetBaseName(string storeType) var openParen = storeType.IndexOf("(", StringComparison.Ordinal); if (openParen >= 0) { - storeType = storeType.Substring(0, openParen); + storeType = storeType[..openParen]; } return storeType; diff --git a/src/EFCore.Relational/Storage/RelationalTypeMappingSource.cs b/src/EFCore.Relational/Storage/RelationalTypeMappingSource.cs index 7e701215b76..6590b5b9954 100644 --- a/src/EFCore.Relational/Storage/RelationalTypeMappingSource.cs +++ b/src/EFCore.Relational/Storage/RelationalTypeMappingSource.cs @@ -474,7 +474,7 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo) var openParen = storeTypeName.IndexOf("(", StringComparison.Ordinal); if (openParen > 0) { - var storeTypeNameBase = storeTypeName.Substring(0, openParen).Trim(); + var storeTypeNameBase = storeTypeName[..openParen].Trim(); var closeParen = storeTypeName.IndexOf(")", openParen + 1, StringComparison.Ordinal); if (closeParen > openParen) { diff --git a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs index b802b5a6531..cc1e7cc98d5 100644 --- a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs +++ b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs @@ -960,7 +960,7 @@ private static string ExpandFileName(string fileName) dataDirectory = AppDomain.CurrentDomain.BaseDirectory; } - fileName = Path.Combine(dataDirectory, fileName.Substring("|DataDirectory|".Length)); + fileName = Path.Combine(dataDirectory, fileName["|DataDirectory|".Length..]); } return Path.GetFullPath(fileName); diff --git a/src/EFCore.SqlServer/Query/Internal/TemporalTableExpression.cs b/src/EFCore.SqlServer/Query/Internal/TemporalTableExpression.cs index 213a7bdb397..cbf4e451bd6 100644 --- a/src/EFCore.SqlServer/Query/Internal/TemporalTableExpression.cs +++ b/src/EFCore.SqlServer/Query/Internal/TemporalTableExpression.cs @@ -20,7 +20,7 @@ public abstract class TemporalTableExpression : TableExpressionBase, IClonableTa /// doing so can result in application failures when updating to a new Entity Framework Core release. /// protected TemporalTableExpression(ITableBase table) - : base(table.Name.Substring(0, 1).ToLowerInvariant()) + : base(table.Name[..1].ToLowerInvariant()) { Name = table.Name; Schema = table.Schema; diff --git a/src/EFCore/Diagnostics/LoggerCategory.cs b/src/EFCore/Diagnostics/LoggerCategory.cs index 4c80ab6e369..78ff66f8762 100644 --- a/src/EFCore/Diagnostics/LoggerCategory.cs +++ b/src/EFCore/Diagnostics/LoggerCategory.cs @@ -40,7 +40,7 @@ private static string ToName(Type loggerCategoryType) var index = name.IndexOf(outerClassName, StringComparison.Ordinal); if (index >= 0) { - name = name.Substring(0, index) + name[(index + outerClassName.Length)..]; + name = name[..index] + name[(index + outerClassName.Length)..]; } return name; diff --git a/src/EFCore/Extensions/Internal/TypeExtensions.cs b/src/EFCore/Extensions/Internal/TypeExtensions.cs index b8808b7c38d..4ea23b8fd63 100644 --- a/src/EFCore/Extensions/Internal/TypeExtensions.cs +++ b/src/EFCore/Extensions/Internal/TypeExtensions.cs @@ -43,7 +43,7 @@ public static string GenerateParameterName(this Type type) var sb = new StringBuilder(); var removeLowerCase = sb.Append(type.Name.Where(char.IsUpper).ToArray()).ToString(); - return removeLowerCase.Length > 0 ? removeLowerCase.ToLowerInvariant() : type.Name.ToLowerInvariant().Substring(0, 1); + return removeLowerCase.Length > 0 ? removeLowerCase.ToLowerInvariant() : type.Name.ToLowerInvariant()[..1]; } /// diff --git a/src/EFCore/Metadata/Conventions/BackingFieldConvention.cs b/src/EFCore/Metadata/Conventions/BackingFieldConvention.cs index c796aaefd6d..e68c47f3bb6 100644 --- a/src/EFCore/Metadata/Conventions/BackingFieldConvention.cs +++ b/src/EFCore/Metadata/Conventions/BackingFieldConvention.cs @@ -164,7 +164,7 @@ private static void DiscoverField(IConventionPropertyBaseBuilder conventionPrope match = TryMatch(sortedFields, propertyName, "", "", propertyBase, null, entityClrType, propertyName); var camelPrefix = char.ToLowerInvariant(propertyName[0]).ToString(); - var camelizedSuffix = propertyName.Substring(1); + var camelizedSuffix = propertyName[1..]; match = TryMatch(sortedFields, camelPrefix, camelizedSuffix, "", propertyBase, match, entityClrType, propertyName); match = TryMatch(sortedFields, "_", camelPrefix, camelizedSuffix, propertyBase, match, entityClrType, propertyName); diff --git a/src/EFCore/Metadata/Internal/PropertyParameterBindingFactory.cs b/src/EFCore/Metadata/Internal/PropertyParameterBindingFactory.cs index b1469496459..5359693ab01 100644 --- a/src/EFCore/Metadata/Internal/PropertyParameterBindingFactory.cs +++ b/src/EFCore/Metadata/Internal/PropertyParameterBindingFactory.cs @@ -32,7 +32,7 @@ public class PropertyParameterBindingFactory : IPropertyParameterBindingFactory private static IList GetCandidatePropertyNames(string parameterName) { - var pascalized = char.ToUpperInvariant(parameterName[0]) + parameterName.Substring(1); + var pascalized = char.ToUpperInvariant(parameterName[0]) + parameterName[1..]; return new List {