Encoding.ASCII vs new ASCIIEncoding ()

If I use:

A) var targetEncodingA = Encoding.ASCII;

and

B) var targetEncodingB = new ASCIIEncoding();

then both targetEncoding0 and targetEncoding1 are of the same type.

Are there any preferred scenarios and / or advantages / disadvantages when using A or B?

(besides creating a new instance with the constructor every time I use it)

+4
source share
2 answers

Here is the implementation detail Encoding.ASCII(from Encoding.cs):

private static volatile Encoding asciiEncoding;

public static Encoding ASCII
{
  {
    if (Encoding.asciiEncoding == null)
      Encoding.asciiEncoding = (Encoding) new ASCIIEncoding();
    return Encoding.asciiEncoding;
  }
}

The main difference is that the type of the return value is different from what type you want to use ( ASCIIEncodingvs Encoding), and the Encodingbase class.

Encoding.ASCII .

+8

Encoding.ASCII , . , (singleton).

Personnaly, , , . , Encoding.ASCII , ASCIIEncoding().

+3

All Articles