Segna il video come già completato  
Se vuoi, sarebbe utile un giudizio su questo video    
In questo video vedremo un'altra novità di Visual Studio 2010, cioè il nuovo metodo Clear() della classe StringBuilder. In Visual Studio 2008, quando dovevamo lavorare con un oggetto StringBuilder, non potevamo ripulirlo da tutti i caratteri con una semplice chiamata ad un metodo (come è appunto il nuovo metodo Clear()). Viceversa dovevamo utilizzare degli espedienti alternativi. Adesso ne vedremo qualcuno. Consideriamo il seguente blocco di codice che ci presenta una prima iterazione sui numeri da 1 a 10. Ad ogni ciclo il numero intero viene convertito in stringa, tramite il metodo ToString() e viene aggiunto all'oggetto StringBuilder tramite il metodo Append(). La seconda iterazione invece viene fatta su tutte le lettere dell'alfabeto dalla A alla Z. Anche in questo caso ogni lettera viene inclusa nell'oggetto StringBuilder tramite il metodo Append(). class Program { static void Main(string[] args) { //iterazione dei numeri da 1 a 10 StringBuilder SB = new StringBuilder(); for (int i = 1; i <= 10; i++) { SB.Append(i.ToString() ); } //stampa a video il risultato dell'iterazione Console.WriteLine(SB.ToString()); //iterazione delle lettere dalla A alla Z for (int chrAlf = 65; chrAlf <= 90; chrAlf++) { //aggiunge alla collection alfabeto la lettera corrente SB.Append(Convert.ToChar(chrAlf).ToString() ); } //stampa a video il risultato dell'iterazione Console.WriteLine(SB.ToString()); //attende l'input dell'utente Console.ReadKey(); } } Proviamo ad avviare questo codice e vediamo che, come è naturale aspettarsi, nel risultato della seconda iterazione sono rimasti anche i risultati della prima. Questo perchè non abbiamo provveduto a ripulire lo StringBuilder nel passaggio da un ciclo all'altro. In Visual Studio 2008, per ripulire lo StringBuilder dovevamo aggiungere questa riga di codice: SB.Remove(0, SB.Length); che utilizza il metodo Remove per rimuovere tutti i caratteri dello StringBuilder, dal carattere 0 all'ultimo. Un secondo metodo per ripulire lo StringBuilder è sostituire la stringa di testo in esso contenuta con una stringa vuota: SB.Replace(SB.ToString(),""); Infine potevamo settare la dimensione dello StringBuilder a 0: SB.Length = 0; Adesso abbiamo a disposizione il metodo Clear() SB.Clear(); che è in tutto e per tutto equivalente al metodo che abbiamo visto prima col quale cioè settavamo la lunghezza pari a 0. Quindi in poche parole il metodo Clear() è un metodo di convenienza nel senso che non introduce nulla di nuovo ma ci consente di chiamare un metodo specifico per il nostro scopo, invece di riadattare altre funzioni al nostro caso. Possiamo verificare il funzionamento interno del metodo Clear() aprendo il solito Reflector di RedGate. Andiamo nel Framework 4.0, mscorlib, CommonLanguageRuntimeLibrary, System.Text, StringBuilder e scegliamo il metodo Clear. Poi dal menu tools clicchiamo su disassemble e scopiramo l'implementazione del metodo: public StringBuilder Clear() { this.Length = 0; return this; } cioè, come detto poco fa, viene settata a 0 la lunghezza dello StringBuilder. In questo video vedremo un'altra novità di Visual Studio 2010 cioè un nuovo metodo Clear() della classe StringBuilder. In Visual studio 2008, quando dovevamo lavorare con un certo StringBuilder, non potevamo ripulirlo da tutti caratteri con una semplice chiamata ad un metodo, come appunto quella al nuovo metodo Clear(). Viceversa dovevamo utilizzare degli espedienti alternativi. Adesso vediamo perchè. Consideriamo questo blocco di codice: è suddiviso in due parti, in due sotto blocchi il primo ha effetto di un'iterazione sui numeri da uno a 10. Ad ogni ciclo di questa iterazione il numero viene convertito in stringa con il metodo ToString() e viene quindi aggiunto all'oggetto stringa il tramite il metodo Append(). La riga finale di questo primo blocco non fa altro che stampare a video il risultato dello StringBuilder. Nel secondo blocco di codice invece abbiamo un'iterazione sulle lettere dell'alfabeto. Anche qui ad ogni ciclo ogni lettera viene aggiunta all'oggetto StringBuilder, tramite il metodo Append(). Alla fine viene visualizzato a video il risultato della concatenazione di tutte queste lettere dell'alfabeto. E alla fine si attende l'input dell'utente. Proviamo ad avviare questo codice e vediamo che è, come naturale aspettarsi, il risultato della seconda iterazione. Quindi nella seconda riga sono rimasti anche i risultati della prima iterazione. Cioè una duplicazione di questi numeri da uno a 10, questo perché non abbiamo provveduto a ripulire il nostro StringBuilder nel passaggio dalla prima iterazione cioè dalla prima riga alla seconda. Per risolvere questo problema in Visual studio 2008 ma anche in Visual studio 2010 avremmo dovuto aggiungere questa riga di codice, esattamente a metà tra la prima iterazione e la seconda. Qui non abbiamo fatto altro che utilizzare il metodo Length dell'oggetto StringBuilder per rimuovere tutti caratteri compresi nell'intervallo specificato da questi due parametri cioè dal carattere zero dal primo carattere al carattere ultimo presente nello stringhe leader risultato proprio quello che c'aspettiamo e che desideriamo ce avere la seconda iterazione ripulita dopo aver effettuato la prima iterazione come possiamo vedere qua non c'è traccia nella seconda riga del risultato della prima iterazione. Un altro metodo per ottenere lo stesso risultato è quello di sostituire la stringa di testo contenuta nello StringBuilder con una stringa vuota per cui al posto della riga che abbiamo appena scritto scriviamo cioè effettuiamo la sostituzione del contenuto dello StringBuilder con una stringa vuota. Vediamo se anche in questo caso risulta vincolo che c'aspettiamo ed effettivamente è così. L'ultimo metodo che potevamo e che possiamo attualmente utilizzare per ripulire l'oggetto sin vivere e settare la dimensione dello StringBuilder a zero e settare le proprietà Lenght a zero. Vediamo anche in questo caso il nostro risultato è proprio quello che vogliamo ottenere adesso però con l'introduzione di Visual studio 2010, oltre a questi tre metodi che sono più che altro degli espedienti abbiamo disposizione il metodo Clear() che è un metodo del tutto equivalente al metodo che abbiamo appena visto nel c'è quello con nel quale settavamo la lunghezza dello StringBuilder pari a zero. Quindi in poche parole il metodo Clear() è un metodo di convenienza nel senso che in realtà non introduce nulla di nuovo ma ci consente di chiamare un metodo specifico utile per il nostro scopo invece di adattare altre funzioni. Se siamo curiosi di vedere il funzionamento interno del metodo Clear8(), possiamo aprire il .NET Reflector, puntiamo chiaramente al framework 4.0, apriamo mscorlib, selezioniamo system.Text e scegliamo quindi la classe StringBuilder. Apriamo il nodo StringBuilder e da qui selezioniamo metodo Clear(). A questo punto possiamo premere lo spazio per aprire il codice e possiamo vedere che utilizza sempre al quale l'implementazione reale del metodo clear era anche in questo caso viene assegnata alla proprietà lenght con valore zero per cui è come se noi ritornando al nostro codice scrivessimo e se length è uguale zero risultato effettivamente lo stesso.
In this video we will see another new feature of Visual Studio 2010 that a new method Clear () of the StringBuilder class. In Visual Studio 2008, when we had to work with a StringBuilder, we could not clean it up for all characters with a simple call to a method, as precisely that the new method Clear (). Conversely we had to use gimmicks alternative. Now we see why. Consider this block of code is divided into two parts, in two blocks in the first iteration of effect on the numbers from one to 10. At each cycle of this iteration number is converted to a string with the ToString () and is then added to the string object using the method Append (). The final line of this first block does is print out your results of the StringBuilder. In the second block of code instead we iterate on the letters of the alphabet. Here, too, with each cycle each letter is added to the StringBuilder object, using the method Append (). At the end of the video is displayed on the concatenation of all these letters of the alphabet. And finally waits for the user. Let's start this code and see that it is as natural to expect the outcome of the second iteration. Then in the second line were also the results of the first iteration. That is a duplication of these numbers from one to 10, this because we have not done so to clean up our StringBuilder passing from the first iteration from the first to the second row. To resolve this problem in Visual Studio 2008 Visual Studio 2010 but also we should add this line of code, exactly halfway between the first and second iteration. Here we have not done anything but use the StringBuilder object's length method to remove all characters in the range specified by these two parameters, namely the zero character from the first character to the last character in the string leading result and that's what We expect we want to have cleaned up the second iteration after the first iteration as we can see here there is no trace in the second row of the result of the first iteration. Another way to achieve the same result is to replace the text string contained in the StringBuilder with an empty string instead of the line so we just write that we provide written to replace the contents of the StringBuilder with an empty string. Let's see if in this case is constraint We expect that and you'd be. The last method we could and now we can use to clean up the object right to live and set the size of the StringBuilder to zero and set the Length property to zero. We also see in this case our result is exactly what we want but now with the introduction of Visual Studio 2010, in addition to these three methods that are more gimmicks than anything we have available, the method Clear () method which is a completely equivalent to the method we have just seen is the one with which we set the length of the StringBuilder zero. So in short, the method Clear () method is a convenience in that it does not really introduce anything new, but we can call a specific method useful for our purpose rather than adapting other functions. If we are curious to see the inner workings of the method Clear8 (), we can open. NET Reflector, we aim to clear framework 4.0, open mscorlib, and then click to select System.Text StringBuilder class. Open the node StringBuilder and from there select the method Clear (). At this point we can press space to open the code and we can see that it always uses to which the actual implementation of the method was also clear in this case is assigned to the property with a value of zero length it is as if we return to our code scrivessimo length is equal to zero, and if indeed the same result.