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.
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.
– Eduardo Oliveira
In this case, zero, or would be negative.
– Maniero