maxp
maxp

Reputation: 25141

Am I going mad? c# / static modifier

I have the below code, oddly enough it keeps on returning the same value (even though filename) is different, if i call it more than once in the same request.

Ive just stepped through the code and even stringbytes is exactly the same (i.e. GetBytes(string)) is returning the same value.

    public static string Base64EncodeString(string filename)
    {
        var stringbytes = System.Text.Encoding.Default.GetBytes(filename);
        return Convert.ToBase64String(stringbytes);
    }

Upvotes: 2

Views: 172

Answers (3)

Alex Mendez
Alex Mendez

Reputation: 5150

Did you check the files that you are loading. It is possible that you just copied the file that you are opening, gave it a different name and did not modify the content.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273219

Your code looks correct. If it's not an Encoding issue as suggested by Jon Skeet i would guess that you have a static fileName and/or stringbytes variable somewhere and that the posted code is not 100% the same as the original code.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500235

I suspect you're not seeing what you think you're seeing. That method won't return the same value if you call it with different values of filename... unless you're using characters which aren't supported by Encoding.Default. (I wouldn't suggest using Encoding.Default unless you really want a platform-specific encoding.)

Upvotes: 5

Related Questions