fork(1) download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. var domainObject = new DomainObject("Leri");
  8. var dto = domainObject.ToDTO();
  9.  
  10. Console.WriteLine(dto.Name);
  11. }
  12. }
  13.  
  14. public class DomainObject {
  15. private readonly string _name;
  16.  
  17. public string Name { get { return _name; } }
  18.  
  19. // some useful methods
  20.  
  21. public DomainObject(string name) {
  22. _name = name;
  23. }
  24. }
  25.  
  26. public class DataTransferObject {
  27. public string Name { get; set; }
  28. }
  29.  
  30. public static class ConverterExtension {
  31. public static DataTransferObject ToDTO(this DomainObject domainObject) {
  32. /**
  33. * Always check for null because extension calls are not virtual calls
  34. * so NullReferenceException won\'t be thrown if called on null.
  35. */
  36. if (domainObject == null) {
  37. throw new ArgumentNullException("domainObject");
  38. }
  39.  
  40. return new DataTransferObject {
  41. Name = domainObject.Name
  42. };
  43. }
  44. }
Success #stdin #stdout 0.02s 33872KB
stdin
Standard input is empty
stdout
Leri