Skip to content

Char is casted to int without warning in string.Remove(int startIndex) #114941

Open
@vsfeedback

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.


[severity:It bothers me. A fix would be nice]
Dear ladies and gentlemen,

The Problem:

    public static class InvalidChars
    {
        public const string InvalidNameChars = "";

public static string CleanName(this string input)
        {
            if (string. IsNullOrWhiteSpace(input))
            {
                return input;
            }

var newInput = input;
            foreach (var c in InvalidNameChars)
            {
                newInput = newInput.Remove(c);
            }

return newInput;
        }
    }

I was writing the following piece of code.
I originally wanted to use replace with an empty character, however then I saw Remove in the autocomplete dropdown and decided to try it.
Thankfully I saw the declaration of the function before proceeding:
image.png
There is no override that takes a char.
For some reason it just automatically casts the char into an int without any warnings.
I tried it with a fresh .NET 8 project (We are using .NET 8) as well, no warnings.
My VS Version is 17.13.6

While I don't think it's such an important issue, it could be dangerous if someone were to just write code and trust the editor. (Which I think we are all guilty of nowadays)

Demo.zip

Thank you for your time.

Sincerely,

Istvan Galfi


Original Comments

Feedback Bot on 4/13/2025, 10:13 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Runtimecode-analyzerMarks an issue that suggests a Roslyn analyzeruntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions