A apresentação está carregando. Por favor, espere

A apresentação está carregando. Por favor, espere

C# e Programação Genérica

Apresentações semelhantes


Apresentação em tema: "C# e Programação Genérica"— Transcrição da apresentação:

1 C# e Programação Genérica
Joel Pereira Software Design Engineer WinFS API Team Microsoft Corporation © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

2 3/24/ :27 AM Conteúdo Baseado na versão Visual Studio 2005, Community Release – Maio de 2004 Material disponibilizado pelo time C# Anders Hejlsberg Eric Gunnerson © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

3 Novidades da Linguagem C#
3/24/ :27 AM Novidades da Linguagem C# Programação Genérica Tipos Nullable Métodos Anonimos Iterators Tipos Parciais Classe Estática Entre outras… © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

4 Programação Genérica public class List { private object[] elements;
private int count; public void Add(object element) { if (count == elements.Length) Resize(count * 2); elements[count++] = element; } public object this[int index] { get { return elements[index]; } set { elements[index] = value; } public int Count { get { return count; } public class List<T> { private T[] elements; private int count; public void Add(T element) { if (count == elements.Length) Resize(count * 2); elements[count++] = element; } public T this[int index] { get { return elements[index]; } set { elements[index] = value; } public int Count { get { return count; } List<int> intList = new List<int>(); intList.Add(1); // No boxing intList.Add(2); // No boxing intList.Add("Three"); // Compile-time error int i = intList[0]; // No cast required List intList = new List(); intList.Add(1); intList.Add(2); intList.Add("Three"); int i = (int)intList[0]; List intList = new List(); intList.Add(1); // Argument is boxed intList.Add(2); // Argument is boxed intList.Add("Three"); // Should be an error int i = (int)intList[0]; // Cast required © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

5 Programação Genérica Porque? Como é implementado?
Verificação de tipos em tempo de compilação Desempenho Redução de código Como é implementado? Instanciado em tempo de execução, não compilação Verificação na declaração, não na instanciação Funciona para tipos referências e valores Informação em tempo de execução -usa o compilador para verificacao forte de tipos, mais produtividade, detecacao de erros antecipada -Elimina boxing/unboxing, elimina downcasts -typed collections, outras api do tipo SqlDataReader -Demo para implementacao © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

6 Programação Genérica Parametros tipo podem ser especificados para
Class, struct, interface, e delegate class Dictionary<K,V> {…} struct HashBucket<K,V> {…} interface IComparer<T> {…} delegate R Function<A,R>(A arg); Dictionary<string,Customer> customerLookupTable; Dictionary<string,List<Order>> orderLookupTable; Dictionary<string,int> wordCount; © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

7 Programação Genérica Parametros tipo podem ser especificados para
Class, struct, interface, e delegate Métodos class Utils { public static T[] CreateArray<T>(int size) { return new T[size]; } public static void SortArray<T>(T[] array) { string[] names = Utils.CreateArray<string>(10); names[0] = "Jones"; Utils.SortArray(names); © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

8 Programação Genérica Parametros tipo podem ser especificados para
Class, struct, interface, e delegate Métodos Parametros tipo podem ter constraints class Dictionary<K,V>: IDictionary<K,V> where K: IComparable<K> where V: IKeyProvider<K>, IPersistable, new() { public void Add(K key, V value) { } class Dictionary<K,V> where K: IComparable { public void Add(K key, V value) { if (key.CompareTo(x) == 0) {…} } class Dictionary<K,V> { public void Add(K key, V value) { if (((IComparable)key).CompareTo(x) == 0) {…} } Os param tipo pode ter regras de validacao que serao verificadas antes de instanciar o tipo © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

9 Programação Genérica Constraint Primaria Constraint Secondaria
"class", or "struct" Constraint Secondaria Interface ou parametro tipo Constraint de instanciação "new()" class Link<T> where T: class {…} class Nullable<T> where T: struct {…} class Relation<T,U> where T: class where U: T {…} © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

10 Programação Genérica Collection classes Collection interfaces
List<T> Dictionary<K,V> SortedDictionary<K,V> Stack<T> Queue<T> Collection classes Collection interfaces Collection base classes Utility classes Reflection IList<T> IDictionary<K,V> ICollection<T> IEnumerable<T> IEnumerator<T> IComparable<T> IComparer<T> Collection<T> KeyedCollection<T> ReadOnlyCollection<T> Nullable<T> EventHandler<T> Comparer<T> © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

11 Demo Programação Genérica

12 Tipo Nullable System.Nullable<T>
Possibilita que um tipo valor seja null É uma struct que associa um T e um bool public struct Nullable<T> where T: struct { public Nullable(T value) {…} public T Value { get {…} } public bool HasValue { get {…} } } Nullable<int> x = new Nullable<int>(123); if (x.HasValue) Console.WriteLine(x.Value);

13 Tipo Nullable Síntaxe T? Literal null Lifted conversions
3/24/ :27 AM Tipo Nullable int? x = 123; double? y = 1.0; Síntaxe T? Literal null Lifted conversions Lifted operators Operador ?? int? x = null; double? y = null; int i = 123; int? x = i; double? y = x; int? z = (int?)y; int j = (int)z; int? x = GetNullableInt(); int? y = GetNullableInt(); int? z = x + y; int? x = GetNullableInt(); int? y = GetNullableInt(); int z = (x + y) / (x – y) ?? 0; © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

14 Métodos Anonimos class MyForm : Form { ListBox listBox;
TextBox textBox; Button addButton; public MyForm() { listBox = new ListBox(...); textBox = new TextBox(...); addButton = new Button(...); addButton.Click += delegate { listBox.Items.Add(textBox.Text); }; } class MyForm : Form { ListBox listBox; TextBox textBox; Button addButton; public MyForm() { listBox = new ListBox(...); textBox = new TextBox(...); addButton = new Button(...); addButton.Click += new EventHandler(AddClick); } void AddClick(object sender, EventArgs e) { listBox.Items.Add(textBox.Text);

15 Métodos Anonimos Permite bloco de código ao inves do delegate
Infêrencia automatica do tipo de delegate Bloco de código sem uso de parametros Ou com parametros Em ambos os casos, tipo de retorno deve ser igual button.Click += delegate { MessageBox.Show("Hello"); }; button.Click += delegate(object sender, EventArgs e) { MessageBox.Show(((Button)sender).Text); };

16 Métodos Anonimos Bloco de código pode acessar variáveis locais
3/24/ :27 AM Métodos Anonimos Bloco de código pode acessar variáveis locais public class Bank { List<Account> GetLargeAccounts(double minBalance) { Helper helper = new Helper(); helper.minBalance = minBalance; return accounts.FindAll(helper.Matches); } internal class Helper internal double minBalance; internal bool Matches(Account a) { return a.Balance >= minBalance; } } public class Bank { List<Account> accounts; List<Account> GetOverdrawnAccounts() { return accounts.FindAll(delegate(Account a) { return a.Balance < 0; }); } List<Account> GetLargeAccounts(double minBalance) { return a.Balance >= minBalance; delegate bool Predicate<T>(T item); public class List<T> { public List<T> FindAll(Predicate<T> filter) { List<T> result = new List<T>(); foreach (T item in this) { if (filter(item)) result.Add(item); } return result; - E as mudancas que o delegate anonimo fizer nas variaveis locais, sera perdidas? © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

17 Métodos Anonimos Converção de métodos Tipo inferido quando possivel
using System; using System.Threading; class Program { static void Work() {…} static void Main() { Thread t = new Thread(Work); t.Start(); } using System; using System.Threading; class Program { static void Work() {…} static void Main() { Thread t = new Thread(new ThreadStart(Work)); t.Start(); }

18 Iterators foreach depende “enumerator pattern”
Método GetEnumerator() foreach faz uma enumeração ficar fácil Mas enumeradores são dificeis de escrever! foreach (object obj in list) { DoSomething(obj); } Enumerator e = list.GetEnumerator(); while (e.MoveNext()) { object obj = e.Current; DoSomething(obj); }

19 Iterators public class ListEnumerator : IEnumerator { List list;
int index; internal ListEnumerator(List list) { this.list = list; index = -1; } public bool MoveNext() { int i = index + 1; if (i >= list.count) return false; index = i; return true; public object Current { get { return list.elements[index]; } public class List { internal object[] elements; internal int count; public ListEnumerator GetEnumerator() { return new ListEnumerator(this); }

20 Iterators Método que computa e retorna uma sequência de valores incrementalmente yield return e yield break Deve retornar um IEnumerator ou IEnumerable public IEnumerator GetEnumerator() { return new __Enumerator(this); } private class __Enumerator: IEnumerator { object current; int state; public bool MoveNext() { switch (state) { case 0: … case 1: … case 2: … public object Current { get { return current; } public class List { public IEnumerator GetEnumerator() { for (int i = 0; i < count; i++) { yield return elements[i]; }

21 Iterators public class List<T> {
public IEnumerator<T> GetEnumerator() { for (int i = 0; i < count; i++) yield return elements[i]; } public IEnumerable<T> Descending() { for (int i = count - 1; i >= 0; i--) yield return elements[i]; public IEnumerable<T> Subrange(int index, int n) { for (int i = 0; i < n; i++) yield return elements[index + i]; List<Item> items = GetItemList(); foreach (Item x in items) {…} foreach (Item x in items.Descending()) {…} foreach (Item x in Items.Subrange(10, 20)) {…}

22 Demo

23 Tipos Parciais public partial class Customer { private int id;
private string name; private string address; private List<Orders> orders; } public class Customer { private int id; private string name; private string address; private List<Orders> orders; public void SubmitOrder(Order order) { orders.Add(order); } public bool HasOutstandingOrders() { return orders.Count > 0; public partial class Customer { public void SubmitOrder(Order order) { orders.Add(order); } public bool HasOutstandingOrders() { return orders.Count > 0;

24 Classes Estáticas Pode conter apenas membros estáticos
Não pode ser tipo de variável, parâmetro, … System.Console, System.Environment, … public static class Math { public static double Sin(double x) {…} public static double Cos(double x) {…} }

25 Acesso à Propriedades Diferentes níveis de acesso para Get/Set
Típica configuração set {…} mais restrito que get {…} public class Customer { private string id; public string CustomerId { get { return id; } internal set { id = value; } }

26 Aliases Externos Assemblies com nomes de tipo idênticos
Versões diferentes do mesmo assembly foo.dll bar.dll extern alias Foo; extern alias Bar; class Program { static void Main() { Foo.Stuff.Utils.F(); Bar.Stuff.Utils.F(); } namespace Stuff { public class Utils public static void F() {…} } namespace Stuff { public class Utils public static void F() {…} } C:\>csc /r:Foo=foo.dll /r:Bar=bar.dll test.cs

27 Namespace Alias Qualifiers
A::B procura por A como namespace alias global::X procura X no namespace global using IO = System.IO; class Program { static void Main() { IO::Stream s = new IO::File.OpenRead("foo.txt"); global::System.Console.WriteLine("Hello"); }

28 Controle de Warning #pragma warning using System; class Program {
[Obsolete] static void Foo() {} static void Main() { #pragma warning disable 612 Foo(); #pragma warning restore 612 }

29 Threading Semaphore Resource counting for mutex’s
3/24/ :27 AM Threading Semaphore Resource counting for mutex’s // construct a semaphore with a count of 3 Semaphore sem = new Semaphore(3); private void UseResource { // Wait until a count is available… sem.WaitOne(); //  the sem count is reduced by one // other code … // Release the count: sem.Release(); //  the count is incremented by one Semaphore is basically a resource-awareness solution, which customers asked for, so we’re giving it to them. The basic concept here is that you have a mutex, but the resource represented by the mutex has a multiple ‘count’. Consider for example, that you want to allow multiple objects refer to, and use serial-ports. Serial Ports are pretty scarce resources, you generally only have two or three on a machine. In this situation, you want to avoid collisions with regards to the instances attempted access of the scarce resource, or else, they may simply fail (or interfere with eachother!). Mutex won’t do, because it locks a single resource (note, a mutex is not dis-similar from a semaphore with a count of 1). Therefore, we use a Semaphore. We construct our Semaphore with a count, being the total number of resources represented. When each instance that needs to share the resources represented by the Semaphore needs that resource, it calls WaitOne on the Semaphore (Wait for a count to be available). If a count is available (the sempahores internal count) then it is given to the instance (and the resource is locked, all in atomic fashion), and the count inside the semaphore is reduced by one. If no count is available for a specific request, then the instance is blocked on the ‘WaitOne’ request, until a count is available. And finally, when each resource is done with it’s use of the resource, it must Release on the semaphore, to let the count go up by one. All pretty straightforward, and very useful for constrained resources! © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

30 Threading Named Events
V1.0 and V1.1: Events can only signal the same process Whidbey: Named Events allow cross process communication Public Class EventWaitHandle Inherits WaitHandle Public Sub New EventWaitHandle(initialState As Boolean, type As EventType, name As String ) © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

31 Longhorn and WinFX: Re-Invigorating the Virtuous Cycle
3/24/ :27 AM Longhorn and WinFX: Re-Invigorating the Virtuous Cycle Applies .NET Framework goodness to core OS Consistent, object oriented Secure, reliable platform Modern library and language designs Enables new class of applications all built on managed code Fuels ecosystems for the next decade of growth © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

32 3/24/ :27 AM For More Information Applied Microsoft .NET Framework Programming (Richter) NET Framework Standard Library Annotated Reference, Vol. 1 (Abrams) © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

33 Outras novidades Friend assemblies Delegate co- and contra-variance
Overloading on type parameter count

34 3/24/ :27 AM Q & A © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

35 © 2004 Microsoft Corporation. All rights reserved.
3/24/ :27 AM © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary. © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.


Carregar ppt "C# e Programação Genérica"

Apresentações semelhantes


Anúncios Google