Advertisement
fcamuso

Framework: supporto per tipo string - A

May 3rd, 2021
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace fw_supportoString
  6. {
  7.   class Program
  8.   {
  9.     static void Main(string[] args)
  10.     {
  11.       string s = new String('*', 80);
  12.       Console.WriteLine(s);
  13.  
  14.       string s1 = null;
  15.       //Console.WriteLine(s1.Length); NO, l'oggetto non รจ istanziato -> eccezione!
  16.  
  17.       s1 = "";
  18.       Console.WriteLine(s1.Length);
  19.  
  20.       if (string.IsNullOrEmpty(s1))
  21.         Console.WriteLine("Stringa non utilizzabile");
  22.  
  23.       s1 = "Topolino & ";
  24.       string s2 = "Minnie";
  25.       string s3 = s1 + s2 + s1 + s2;
  26.       string s4 = String.Concat(s1, s2, s1, s2);
  27.  
  28.       Cliente cliente1 = new Cliente(); ;
  29.       string s5 = String.Concat(cliente1, cliente1);
  30.       Console.WriteLine(s5);
  31.  
  32.       List<string> elenco = new List<string> { "uno ", "due " , "tre "};
  33.       s5 = String.Concat(elenco);
  34.       Console.WriteLine(s5);
  35.  
  36.       DateTime start = DateTime.Now;
  37.       s5 = "";
  38.       for (int i = 0; i < 50000; i++)
  39.         s5 = String.Concat(s5, " abc ");
  40.       DateTime end = DateTime.Now;
  41.       Console.WriteLine($"Tempo - Concat: {end - start}");
  42.  
  43.       start = DateTime.Now;
  44.       s5 = "";
  45.  
  46.       StringBuilder sb = new StringBuilder();
  47.       for (int i = 0; i < 1000000; i++)
  48.         sb.Append(" abc ");
  49.       end = DateTime.Now;
  50.       Console.WriteLine($"Tempo - StrBld: {end - start}");
  51.  
  52.       start = DateTime.Now;
  53.       int n = 10;
  54.       s5 = String.Concat("ciao a ", n, "persone ", n, " volte!");
  55.       end = DateTime.Now;
  56.       Console.WriteLine($"Tempo - Concat2: {end - start}");
  57.  
  58.       StringBuilder sb2 = new StringBuilder();
  59.       sb2.Append("ciao a ");
  60.       sb2.Append(n);
  61.       sb2.Append("persone ");
  62.       sb2.Append(n);
  63.       sb2.Append(" volte!");
  64.       end = DateTime.Now;
  65.       Console.WriteLine($"Tempo - StrBld2: {end - start}");
  66.  
  67.     }
  68.   }
  69.  
  70.   class Cliente
  71.   {
  72.     string cognome = "Mario";
  73.     string nome = "Rossi";
  74.     int eta = 36;
  75.     public Cliente() { }
  76.  
  77.     public override string ToString()
  78.     {
  79.       return $"{cognome} {nome} {eta}";
  80.     }
  81.   }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement