Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Question on best practice) Why is "using System.Text" there by default?

Every time I creat a class, I see using System.Text that is added (amongst other using) by default. Every time I remove it after a while because it is unused according to ReSharper.

Am I missing a best practice? Do you use that namespace often? In which situation?

There has to be a reason why this namespace is referenced by default.

Thanks!

like image 900
Mathieu Avatar asked Jun 07 '11 02:06

Mathieu


People also ask

What is the use of using System text in C#?

Text Namespace. Contains classes that represent ASCII and Unicode character encodings; abstract base classes for converting blocks of characters to and from blocks of bytes; and a helper class that manipulates and formats String objects without creating intermediate instances of String.

What are namespaces in C#?

The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types. C# Copy.


2 Answers

The System.Text namespace contains classes, abstract base classes and helper classes. Say for example if you wanted to take advantage of the StringBuilder, Decoder, Encoder, etc....

The classes above, plays a significant role in most cases in .net. But it is not necessary for it to be there in your code. It only applies as to when you needed it. The important thing is to know when you will need the namespace.

It is added in visual studio by default for the convenience of the developers. Same with the System.Linq namespace, not all of the time you will be using it but for your convenience it is already added assuming that you would be using it and that would be up to you to remove it which is by case to case basis.

Sometimes it would be a lot easier to delete it if it is not needed than to figure out the namespace and type when you need it :)

More info regarding System.Text

like image 51
Carls Jr. Avatar answered Oct 01 '22 05:10

Carls Jr.


If it's not being used, it shouldn't be in the code. If Visual Studio adds them by default, chalk that up to Microsoft just trying to make things easy for a developer, as it thinks those are very common namespaces, but depending on what you are doing they likely aren't needed in many classes, just as you experience.

like image 44
Matt Avatar answered Oct 01 '22 05:10

Matt