diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs index 722cc92322ef..e9a106e1068e 100644 --- a/csharp/src/Google.Protobuf/Collections/MapField.cs +++ b/csharp/src/Google.Protobuf/Collections/MapField.cs @@ -590,7 +590,7 @@ object IDictionary.this[object key] IEnumerable IReadOnlyDictionary.Values => Values; #endregion - private class DictionaryEnumerator : IDictionaryEnumerator + private sealed class DictionaryEnumerator : IDictionaryEnumerator { private readonly IEnumerator> enumerator; diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs index fbcc83970720..b420ae615193 100644 --- a/csharp/src/Google.Protobuf/JsonFormatter.cs +++ b/csharp/src/Google.Protobuf/JsonFormatter.cs @@ -85,7 +85,7 @@ public sealed class JsonFormatter { static JsonFormatter() { for (int i = 0; i < CommonRepresentations.Length; i++) { - if (CommonRepresentations[i] == "") { + if (CommonRepresentations[i].Length == 0) { CommonRepresentations[i] = ((char)i).ToString(); } } @@ -291,7 +291,7 @@ private static bool IsDefaultValue(FieldDescriptor descriptor, object value) { return descriptor.FieldType switch { FieldType.Bool => (bool)value == false, FieldType.Bytes => (ByteString)value == ByteString.Empty, - FieldType.String => (string)value == "", + FieldType.String => ((string)value).Length == 0, FieldType.Double => (double)value == 0.0, FieldType.SInt32 or FieldType.Int32 or FieldType.SFixed32 or FieldType.Enum => (int)value == 0, @@ -497,7 +497,7 @@ private void WriteAny(TextWriter writer, IMessage value, int indentationLevel) { WriteBracketClose(writer, ObjectCloseBracket, true, indentationLevel); } - private void WriteDiagnosticOnlyAny(TextWriter writer, IMessage value) { + private static void WriteDiagnosticOnlyAny(TextWriter writer, IMessage value) { string typeUrl = (string)value.Descriptor.Fields[Any.TypeUrlFieldNumber].Accessor.GetValue(value); ByteString data = diff --git a/csharp/src/Google.Protobuf/JsonParser.cs b/csharp/src/Google.Protobuf/JsonParser.cs index fcf859daba09..095875cab326 100644 --- a/csharp/src/Google.Protobuf/JsonParser.cs +++ b/csharp/src/Google.Protobuf/JsonParser.cs @@ -830,7 +830,7 @@ private static void MergeTimestamp(IMessage message, JsonToken token) // TODO: It would be nice not to have to create all these objects... easy to optimize later though. Timestamp timestamp = Timestamp.FromDateTime(parsed); int nanosToAdd = 0; - if (subseconds != "") + if (subseconds.Length != 0) { // This should always work, as we've got 1-9 digits. int parsedFraction = int.Parse(subseconds.Substring(1), CultureInfo.InvariantCulture); @@ -906,7 +906,7 @@ private static void MergeDuration(IMessage message, JsonToken token) { long seconds = long.Parse(secondsText, CultureInfo.InvariantCulture) * multiplier; int nanos = 0; - if (subseconds != "") + if (subseconds.Length != 0) { // This should always work, as we've got 1-9 digits. int parsedFraction = int.Parse(subseconds.Substring(1)); diff --git a/csharp/src/Google.Protobuf/JsonTokenizer.cs b/csharp/src/Google.Protobuf/JsonTokenizer.cs index f89e36adf4df..d12d427d4f9e 100644 --- a/csharp/src/Google.Protobuf/JsonTokenizer.cs +++ b/csharp/src/Google.Protobuf/JsonTokenizer.cs @@ -413,7 +413,7 @@ private double ReadNumber(char initialCharacter) var builder = StringBuilderCache.Acquire(); if (initialCharacter == '-') { - builder.Append("-"); + builder.Append('-'); } else { diff --git a/csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs b/csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs index 7289e44a0f56..1fe9465751e3 100644 --- a/csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs +++ b/csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs @@ -279,7 +279,7 @@ internal IDescriptor LookupSymbol(string name, IDescriptor relativeTo) { // Chop off the last component of the scope. - int dotpos = scopeToTry.ToString().LastIndexOf("."); + int dotpos = scopeToTry.ToString().LastIndexOf('.'); if (dotpos == -1) { result = FindSymbol(name); diff --git a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs index 0654a25f5b28..648b37ad30aa 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs @@ -120,7 +120,7 @@ internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file, // a MapField, but that feels a tad nasty. PropertyName = propertyName; Extension = extension; - JsonName = Proto.JsonName == "" ? JsonFormatter.ToJsonName(Proto.Name) : Proto.JsonName; + JsonName = Proto.JsonName.Length == 0 ? JsonFormatter.ToJsonName(Proto.Name) : Proto.JsonName; } /// diff --git a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs index c7b307b1c85d..773d5c2a8718 100644 --- a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs @@ -63,7 +63,7 @@ static FileDescriptor() private readonly Lazy> declarations; - private FileDescriptor(ByteString descriptorData, FileDescriptorProto proto, IEnumerable dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedClrTypeInfo generatedCodeInfo) + private FileDescriptor(ByteString descriptorData, FileDescriptorProto proto, IList dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedClrTypeInfo generatedCodeInfo) { SerializedData = descriptorData; DescriptorPool = pool; @@ -128,7 +128,7 @@ private IDescriptor FindDescriptorForPath(IList path) return current; } - private DescriptorBase GetDescriptorFromList(IReadOnlyList list, int index) + private static DescriptorBase GetDescriptorFromList(IReadOnlyList list, int index) { // This is fine: it may be a newer version of protobuf than we understand, with a new descriptor // field. @@ -419,7 +419,7 @@ public static FileDescriptor FromGeneratedCode( { ExtensionRegistry registry = new ExtensionRegistry(); registry.AddRange(GetAllExtensions(dependencies, generatedCodeInfo)); - + FileDescriptorProto proto; try {