How should I assign strings in C#?

Asked

Viewed 113 times

4

As far as I know, System.String no. NET (I don’t know if on other platforms the string type is also like this) is immutable, ie if I do:

string a = "texto";
a = "outro texto";

Behind the scenes, by assigning another text in a, it will create another instance of a. Is that right or am I saying something wrong?

There are some parts of the code here in the company with the following scenario:

private void Cadastrar(string Valor, string Valor2, string Valor3)
{
    string msg = "";
    try
    {       
        Entidade obj = new Entidade();
        DAL objDal = new DAL();

        msg = "Campo 1";
        obj.Campo1 = Valor;

        msg = "Campo 2";
        obj.Campo2 = Valo2;

        msg = "Campo 3";
        obj.Campo3 = Valor3;

        objDal.Cadastrar(obj);

    }
    catch(Exception ex)
    {
        string errmsg = "Erro ao cadastrar o campo " + msg;
        MessageBox.Show(errmsg);
    }
}

I imagine that the assignment of strings should be the least of my concerns in this scenario, but is there any way I can improve the attributions of these strings or is that freshness on my part?

My concern is that this happens in several places and I believe that this could be an unnecessary burden on implementation and that it can be corrected or improved in some way.

1 answer

3


Behind the scenes, by assigning another text in a, it will create another instance of a. Is that right or am I saying something wrong?

Not exactly, in this example you are "creating" another instance, but not always this is true, assigning a value to a variable and creating instance are separate things. Although this specific instance is already in the executable, then creating is not quite the term either. Literals of the type string are all already created when you start running the application, an instance is only created in a new memory area when the data comes externally or is built at runtime.

I imagine the attribution of strings should be the least of my worries in this scenario

I don’t know, I care a lot, and I understand the whole functioning of the . NET memory to make the right decisions.

but there is some way that I can improve the attributions of these strings or is that freshness on my part?

It’s worth my first comment. There may be a better way to do this mechanism that you have created as a whole (including because it is the wrong use of exception, but that is not the case here), but done in this way there is no way to reduce the number of instances of existing text. Except perhaps the concatenation of strings in exception, but as I said, this error only exists because the use of exception is wrong.

When concatenates two strings is creating a third, so prefer a interpolation, the win may not be great, but it might be worth it.

It doesn’t affect anything you’re asking, but it could affect another case, why create a variable that’s only used once? In general this is a misunderstanding.

My concern is that it happens in many places.

This can be a problem, everything that happens in various places tends to be the wrong mechanism and probably violates the DRY.

  • From what I understand, using Stringbuilder wouldn’t help much, right? The problem is much bigger, ie in the concept and not simply I exchange A for B.

  • 1

    In this case, zero, or would be negative.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.