Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlbertoMonteiro/c537877de25aee0108d3 to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/c537877de25aee0108d3 to your computer and use it in GitHub Desktop.
Comparativo C# 5 e C# 6
using System;
namespace ConsoleApplication2
{
public class ExceptionCSharp5 : Exception
{
public ExceptionCSharp5()
: base("Mensagem")
{ }
}
}
using System;
namespace ConsoleApplication2
{
public class ExceptionCSharp6() : Exception("Mensagem") { }
}
namespace ConsoleApplication2
{
public class PessoaCSharp5
{
public PessoaCSharp5(string primeiroNome, string segundoNome, int idade)
{
PrimeiroNome = primeiroNome;
SegundoNome = segundoNome;
Idade = idade;
}
public int Idade { get; set; }
public string PrimeiroNome { get; set; }
public string SegundoNome { get; set; }
public string NomeCompleto
{
get
{
return string.Format("{0} {1}", PrimeiroNome, SegundoNome);
}
}
public override string ToString()
{
return string.Format("{0} tem {1} anos", NomeCompleto, Idade);
}
}
}
namespace ConsoleApplication2
{
public class PessoaCSharp6(string primeiroNome, string segundoNome, int idade)
{
public int Idade { get; set; } = idade;
public string PrimeiroNome { get; set; } = primeiroNome;
public string SegundoNome { get; set; } = segundoNome;
public string NomeCompleto => string.Format("{0} {1}", PrimeiroNome, SegundoNome);
public override string ToString() => string.Format("{0} tem {1} anos", NomeCompleto, Idade);
}
}
using System;
using System.Net;
namespace ConsoleApplication2
{
public class TrataExcessaoCSharp5
{
public void BaixaAlgo()
{
var request = (HttpWebRequest)HttpWebRequest.Create("http://www2.enderecoquenaoexiste.com");
try
{
var response = request.GetResponse();
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.Timeout)
Console.WriteLine("Timeout");
else if (ex.Status == WebExceptionStatus.ConnectFailure)
Console.WriteLine("ConnectFailure");
else if (ex.Status == WebExceptionStatus.NameResolutionFailure)
Console.WriteLine("NameResolutionFailure");
}
}
}
}
using System;
using System.Net;
namespace ConsoleApplication2
{
class TrataExcessaoCSharp6
{
public void BaixaAlgo()
{
var request = (HttpWebRequest)HttpWebRequest.Create("http://www2.enderecoquenaoexiste.com");
try
{
var response = request.GetResponse();
}
catch (WebException ex) if (ex.Status == WebExceptionStatus.Timeout)
{
Console.WriteLine("Timeout");
}
catch(WebException ex) if (ex.Status == WebExceptionStatus.ConnectFailure)
{
Console.WriteLine("ConnectFailure");
}
catch (WebException ex) if (ex.Status == WebExceptionStatus.NameResolutionFailure)
{
Console.WriteLine("NameResolutionFailure");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment