1 / 33

C # 4

C # 4. Effective Code. How did we get here ?. The Path to C# 4. public PersonViewModel []  SynchroniseDisplay ( Person [] persons, PersonViewModel []  personsDisplay ) { PersonViewModel [] result =  new PersonViewModel [ persons.Length ];

tyrone
Download Presentation

C # 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C# 4 Effective Code

  2. How did we get here? The Path to C# 4

  3. publicPersonViewModel[] SynchroniseDisplay( Person[] persons, PersonViewModel[] personsDisplay) { PersonViewModel[] result = newPersonViewModel[persons.Length]; for (inti = 0; i < persons.Length; i++)    { Person p = persons[i]; PersonViewModel model = null; for (int j = 0; j < personsDisplay.Length; j++)        { PersonViewModel m = personsDisplay[j]; if (m.Subject.Id == p.Id)            {                model = m; break;            }        } if (model == null)        {            model = newPersonViewModel(p);        } model.Subject = p;        result[i] = model;    } return result; }

  4. publicPersonViewModel[] SynchroniseDisplay( Person[] persons, PersonViewModel[] personsDisplay) { PersonViewModel[] result = newPersonViewModel[persons.Length]; int index = 0; foreach (Person p in persons) { PersonViewModel model = null; foreach (PersonViewModel m in personsDisplay) { if (m.Subject.Id == p.Id) {                 model = m; break; } } if (model == null) {             model = newPersonViewModel(p); } model.Subject = p;         result[index++] = model; } return result; }

  5. publicPersonViewModel[] SynchroniseDisplay( Person[] persons, PersonViewModel[] personsDisplay) { PersonViewModel[] result = newPersonViewModel[persons.Length]; int index = 0; foreach (Person p in persons) { PersonViewModel model = null; foreach (PersonViewModel m inpersonsDisplay) { if (m.Subject.Id == p.Id) {                 model = m; break; } } if (model == null) {             model = newPersonViewModel(p); } model.Subject = p;         result[index++] = model; } return result; }

  6. publicList<PersonViewModel>SynchroniseDisplay( List<Person> persons, List<PersonViewModel>personsDisplay) { List<PersonViewModel> result = newList<PersonViewModel>(persons.Count); foreach (Person p in persons) { PersonViewModel model = null; foreach (PersonViewModel m inpersonsDisplay) { if (m.Subject.Id == p.Id) {                 model = m; break; } } if (model == null) {             model = newPersonViewModel(p); } model.Subject = p; result.Add(model); } return result; }

  7. publicList<PersonViewModel>SynchroniseDisplay( List<Person> persons, List<PersonViewModel>personsDisplay) { List<PersonViewModel> result = newList<PersonViewModel>(persons.Count); foreach (Person p in persons) { PersonViewModel model = null; foreach (PersonViewModel m in personsDisplay) { if (m.Subject.Id == p.Id) {                 model = m; break; } } if (model == null) {             model = newPersonViewModel(p); } model.Subject = p; result.Add(model); } return result; }

  8. publicList<PersonViewModel>SynchroniseDisplay( List<Person> persons, List<PersonViewModel>personsDisplay) { List<PersonViewModel> result  =newList<PersonViewModel>(persons.Count); foreach (Person p in persons) { PersonViewModel model = null; foreach (PersonViewModel m inpersonsDisplay) { if (m.Subject.Id == p.Id) {                 model = m; break; } }         model = model ?? newPersonViewModel(p); model.Subject = p; result.Add(model); } return result; }

  9. publicList<PersonViewModel>SynchroniseDisplay( List<Person> persons, List<PersonViewModel>personsDisplay) { List<PersonViewModel> result  =newList<PersonViewModel>(persons.Count); foreach (Person p in persons) { PersonViewModel model = null; foreach (PersonViewModel m inpersonsDisplay) { if (m.Subject.Id == p.Id) {                 model = m; break; } }         model = model ?? newPersonViewModel(p); model.Subject = p; result.Add(model); } return result; }

  10. publicList<PersonViewModel>SynchroniseDisplay( List<Person> persons, List<PersonViewModel>personsDisplay) { var result = newList<PersonViewModel>(persons.Count); foreach (var p in persons) { PersonViewModel model = null; foreach (var m inpersonsDisplay) { if (m.Subject.Id == p.Id) {                 model = m; break; } }         model = model ?? newPersonViewModel(p); model.Subject = p; result.Add(model); } return result; }

  11. publicList<PersonViewModel> SynchroniseDisplay( List<Person> persons, List<PersonViewModel>personsDisplay) { var result = newList<PersonViewModel>(persons.Count); foreach (var p in persons) { PersonViewModel model = null; foreach (var m inpersonsDisplay) { if (m.Subject.Id == p.Id) {                 model = m; break; } }         model = model ?? newPersonViewModel(p); model.Subject = p; result.Add(model); } return result; }

  12. publicList<PersonViewModel> SynchroniseDisplay( List<Person> persons, List<PersonViewModel>personsDisplay) { var result = newList<PersonViewModel>(persons.Count); foreach (var p in persons)     { var model  = personsDisplay.FirstOrDefault( m => m.SubjectId == p.Id);         model = model ?? newPersonViewModel(p); model.Subject = p; result.Add(model);     } return result; }

  13. publicList<PersonViewModel> SynchroniseDisplay( List<Person> persons, List<PersonViewModel>personsDisplay) { var result = newList<PersonViewModel>(persons.Count); foreach (var p in persons)     { var model  = personsDisplay.FirstOrDefault( m => m.SubjectId == p.Id);         model = model ?? newPersonViewModel(p); model.Subject = p; result.Add(model);     } return result; }

  14. publicIEnumerable<PersonViewModel>SynchroniseDisplay( IEnumerable<Person> persons, IEnumerable<PersonViewModel>personsDisplay) { var result  =newList<PersonViewModel>(persons.Count()); foreach (var p in persons) { varmodel             = personsDisplay.FirstOrDefault( m => m.Subject.Id == p.Id);         model = model ?? newPersonViewModel(p); model.Subject = p; result.Add(model); } return result; }

  15. publicIEnumerable<PersonViewModel>SynchroniseDisplay( IEnumerable<Person> persons, IEnumerable<PersonViewModel>personsDisplay) { var result  =newList<PersonViewModel>(persons.Count()); foreach (var p in persons) { varmodel             = personsDisplay.FirstOrDefault( m => m.Subject.Id == p.Id);         model = model ?? newPersonViewModel(p); model.Subject = p; result.Add(model); } return result; }

  16. Then and Now … publicPersonViewModel[] SynchroniseDisplay( Person[] persons, PersonViewModel[] personsDisplay) { PersonViewModel[] result  =newPersonViewModel[persons.Length]; for (inti = 0; i < persons.Length; i++)    { Person p = persons[i]; PersonViewModel model = null; for (int j = 0; j < personsDisplay.Length; j++)        { PersonViewModel m = personsDisplay[j]; if (m.Subject.Id == p.Id)            {                model = m; break;            }        } if (model == null)        {            model = newPersonViewModel(p);        } model.Subject = p;        result[i] = model;    } return result; } publicIEnumerable<PersonViewModel>SynchroniseDisplay( IEnumerable<Person> persons, IEnumerable<PersonViewModel>personsDisplay) { var result  = newList<PersonViewModel>(persons.Count()); foreach (var p in persons)     { var model             = personsDisplay.FirstOrDefault( m => m.Subject.Id == p.Id);         model = model ?? newPersonViewModel(p); model.Subject = p; result.Add(model);     } return result; } 14 lines 2 nested loops Arrays only 8 lines 1 loop Any sequence

  17. Dynamic C# 4

  18. dynamic A way to opt-out of compile time checks some ^ “Trust me, I know what I’m doing”

  19. Calling a Method dynamiccalc = GetCalculator(); intsum = calc.Add(10, 20); objectcalc = GetCalculator(); TypecalcType = calc.GetType(); objectres = calcType.InvokeMember( "Add", BindingFlags.InvokeMethod, null, newobject[] { 10, 20 }); intsum = Convert.ToInt32(res);

  20. Dynamic publicvoidSpeak() { object critter = FindCritter(); critter.Speak(); } publicvoidSpeak() { IAnimalcritter = FindCritter(); critter.Speak(); } publicvoidSpeak() { dynamic critter = FindCritter(); critter.Speak(); }

  21. Getting involved

  22. Dynamic // First open a connection to our database dynamicdb = OpenDatabaseConnection(“sample”); // Query for Smiths var persons = db.FindByFamilyName<Person>(“Smith”); foreach (var person in persons) { // Process a person } db.FindByKnownAs<Person>(“John”); db.FindBySuburb<Address>(“Kelson”); db.FindByCode<Organisation>(“BNZ”);

  23. Optional & Named Parameters C# 4

  24. Excel ChartWizard xlChart.ChartWizard( cellRange.CurrentRegion, Constants.xl3DBar, PlotBy: Excel.XlRowCol.xlColumns, SeriesLabels: 2, CategoryLabels: 1, HasLegend: false, Title: xlSheet.Name); xlChart.ChartWizard( cellRange.CurrentRegion, Constants.xl3DBar, Type.Missing, Excel.XlRowCol.xlColumns, 1, 2, false, xlSheet.Name, Type.Missing, Type.Missing, Type.Missing);

  25. Code Clarity dialog.Display(model, true, false, true, false); public void Display( ViewModelmodel, bool modal, bool resizable,boolshowHeader,boolallowApply){ // ... } public void Display( ViewModel model, bool modal = true, bool resizable = true,    bool showHeader = true,    bool allowApply = true){ // ... } dialog.Display( model, modal: true, resizable: false, showHeader: true, allowApply: false); dialog.Display( model, resizable: false, allowApply: false);

  26. Covariance and Contravariance C# 4

  27. publicvoidPrintList(IEnumerable<Book> books) { // ... } publicvoid Demo() { IEnumerable<Comic>myComics = newList<Comic>(); // ... PrintList(myComic); } In C# 3 this doesn’t work Need Have Book ← Comic IEnumerable<Book> ≠ IEnumerable<Comic>

  28. publicvoidPrintList(IEnumerable<Book> books) { // ... } publicvoid Demo() { IEnumerable<Comic>myComics = newList<Comic>(); // ... PrintList(myComic); } In C# 4 this does work Covariance Need Have Book ← Comic IEnumerable<Book> ← IEnumerable<Comic>

  29. public interface Reader<T> where T : Book { void Read(T item) } publicvoidDemo(Reader<Book> reader) { Reader<Comic> comicReader = reader; } In C# 3 this doesn’t work Need Have Book ← Comic Reader<Comic> ≠ Reader<Book>

  30. public interface Reader<T> where T : Book { void Read(T item) } publicvoidDemo(Reader<Book> reader) { Reader<Comic> comicReader = reader; } In C# 4 this does work Contravariance Need Have Book ← Comic Reader<Comic> → Reader<Book>

  31. Covariance Contravariance Reader<in T> Uses T as an input only Passing a subclass Is always Ok IEnumerable<out T> Uses T as an output only Returning a subclass is always Ok. publicBook GetBook() { returnnew Comic(); } publicvoid Read(Book book) { } Read(newComic()); publicIEnumerable<Book>GetBooks() { IEnumerable<Comic> result = ... return result; } Reader<Comic> reader = newReader<Book>() reader.Read(myComic);

  32. Covariance and Contravariance Mostly, don’t worry about it It means that things should work the way you expect.

  33. Thanks. unrepentantgeek bevan@nichesoftware.co.nz www.nichesoftware.co.nz twitter email blog

More Related