C++ 프로그래머를 위한 C#             ㈜넥슨   신규개발3본부 개발1실 GTR팀 성능연구유닛             김재석
책임연구원                2010/2011, 마비노기2                2006/2009, 마비노기 영웅전                           테크니컬 디렉터               ...
AgendaVisual Studio .NET Rainer                                                 .NET Framework 1.0 C# 1.0Visual Studio .NE...
VS .NET   VS .NET 2003     VS 2005      VS 2008   Rx            Visual Studio .NET Rainer          .NET Framework 1.0 / C#...
VS .NET   VS .NET 2003   VS 2005   VS 2008   Rx                 C# 1.0 – Managed Code                 Common Type System  ...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxCommon Type SystemPrimitive TypesFieldMethodPropertyEvent
VS .NET   VS .NET 2003     VS 2005   VS 2008   RxPrimitive TypesByte                     BooleanInt16                    C...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxField// C#const int constant = 0;readonly int readOnlyField;// C++static co...
VS .NET     VS .NET 2003        VS 2005   VS 2008   RxField (cont.)Field ≠ variabledefault(T) if not initialized• null if ...
VS .NET     VS .NET 2003      VS 2005   VS 2008   RxMethodMember function with CV-qualifierclass Object{public:   String T...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxMethod (cont.)// C#void CallByRef(ref int value);bool TryGetValue(out int v...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxPropertyGet Method/Set Method ≠ reference to type// C#variable = obj.Proper...
VS .NET   VS .NET 2003    VS 2005      VS 2008   RxProperty (cont.)Indexed property ≠ index operator []int this[int index]...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxProperty (cont.)Implementation: lazy evaluation classclass Property<typenam...
VS .NET   VS .NET 2003   VS 2005   VS 2008     RxProperty (cont.)VC 7.1 or higher: property extended storage-  class modif...
VS .NET     VS .NET 2003   VS 2005   VS 2008   RxEventMulticast delegate// C#obj.EventName += someDelegate;obj.EventName -...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxClassesFinalizer ≠ destructorGC.ReRegisterForFinalize(this);GC.SuppressFina...
VS .NET    VS .NET 2003     VS 2005    VS 2008   RxClasses (cont.)Prevent R6025 – Pure virtual function call__forceinline ...
VS .NET         VS .NET 2003            VS 2005   VS 2008   RxClasses (cont.)Type initializer a.k.a. static constructornam...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxClasses (cont.)Sealed classtemplate <typename T>class Sealed{private:   fri...
VS .NET   VS .NET 2003      VS 2005   VS 2008   RxStructuresLightweight – Value typesDefault constructor / finalizer• NEVE...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxEnumerationsValue typesstring[] Enum.GetNames()TEnum[] Enum.GetValues()TEnu...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxInterfacesVirtual inheritancetemplate <>class Task<void> : public Object,  ...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxDelegatesClosures or managed functorsCalling conventions:• cdecl• stdcall• ...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxDelegates (cont.)#define DEFINE_DELEGATE(TPARAMS, PARAMS,  ARGS)template <t...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxAttributestemplate <typename T>class CustomAttribute{public:  static const ...
VS .NET   VS .NET 2003   VS 2005   VS 2008     RxDirectivesusing directive (C#) ≈ using directive (C++)Assemblies are refe...
VS .NET    VS .NET 2003      VS 2005        VS 2008   Rx          Visual Studio .NET 2003 Everett           .NET Framework...
VS .NET   VS .NET 2003    VS 2005   VS 2008   Rx                 C# 1.2 – Bug Fix                 IDisposable interface   ...
VS .NET   VS .NET 2003     VS 2005      VS 2008   Rx           Visual Studio 2005 Whidbey          .NET Framework 2.0 / C#...
VS .NET   VS .NET 2003   VS 2005    VS 2008   Rx                C# 2.0 – Generic                Generics                It...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxGenericsSystem.Collections.Generic• List<T>       std::vector<T>• Queue<T> ...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxIteratorsIEnumerable<T> Where<T>(  IEnumerable<T> source,  Func<T, bool> pr...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxPartial Classes// Generated.Designer.cs[CLSCompliant]partial class Generate...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxNullable TypesNullable<T> where T : structCoalescing operator ??int? nullab...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxAnonymous Methods// C# 2.0Func<Type, bool> predicate =  // nested type in C...
VS .NET    VS .NET 2003    VS 2005    VS 2008   RxStatic ClassesClasses which are both abstract and sealed.public static c...
VS .NET   VS .NET 2003      VS 2005   VS 2008   RxProperty AccessorAccessibilities// C# 1.2public Type PropertyName{ get {...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxDelegatesstring Covariance() …Func<object> function =  new Func<object>(Cov...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxDelegates (cont.)// C# 1.2Action action =  new Action(obj.MethodName);// C#...
VS .NET   VS .NET 2003     VS 2005      VS 2008   Rx            Visual Studio 2008 Orcas          .NET Framework 3.5 / C# ...
VS .NET   VS .NET 2003    VS 2005   VS 2008   Rx                C# 3.0 – LINQ                Implicitly Type Local Variabl...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxImplicitly Type LocalVariables and Arrays// C# 2.0Dictionary<string, object...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxObject Initializers// C# 2.0SomeKeyValue pair = new SomeKeyValue();value.Na...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxCollection Initializers// C# 2.0List<int> list = new List<int>();list.Add(1...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxExtension Methodsstatic class ComponentExtensions{  static void DoSomething...
VS .NET     VS .NET 2003      VS 2005     VS 2008      RxAnonymous Typesforeach (var value in from row in table           ...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxLambda Expressions// C# 2.0Func<Type, bool> predicate =  delegate(Type valu...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxQuery Keywordsvar q = from c in categories        join p in products       ...
VS .NET   VS .NET 2003   VS 2005   VS 2008    RxQuery Keywords (cont.)var q = categories        .Join(products,           ...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxAuto-ImplementedProperties// C# 2.0public Type PropertyName{  get { return ...
VS .NET   VS .NET 2003   VS 2005     VS 2008   RxPartial MethodDefinitions// Generated.Designer.cspartial class Generated{...
VS .NET        VS .NET 2003              VS 2005          VS 2008      Rx More LINQ with System.Interactive – Getting Star...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxReactive ExtensionsTask Parallel Library ∈ .NET Framework 4.0Observer Desig...
VS .NET   VS .NET 2003    VS 2005   VS 2008   RxTask Parallel LibrarySystem.Collections.Concurrent• ConcurrentBag<T>      ...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxTask Parallel Library(cont.)System.Threading.Tasks// C# 3.0ThreadPool.Queue...
VS .NET        VS .NET 2003             VS 2005          VS 2008        RxObserver Design Pattern  More LINQ with System.I...
VS .NET   VS .NET 2003   VS 2005   VS 2008   RxObserver Design Pattern(Further reading)Duality of IEnumerable<T> / IObserv...
Q&Atwitter:@tcaesvk
김재석, C++ 프로그래머를 위한 C#, NDC2011
김재석, C++ 프로그래머를 위한 C#, NDC2011
Upcoming SlideShare
Loading in …5
×

김재석, C++ 프로그래머를 위한 C#, NDC2011

3,613 views

Published on

Published in: Technology
1 Comment
11 Likes
Statistics
Notes
No Downloads
Views
Total views
3,613
On SlideShare
0
From Embeds
0
Number of Embeds
71
Actions
Shares
0
Downloads
0
Comments
1
Likes
11
Embeds 0
No embeds

No notes for slide

김재석, C++ 프로그래머를 위한 C#, NDC2011

  1. 1. C++ 프로그래머를 위한 C# ㈜넥슨 신규개발3본부 개발1실 GTR팀 성능연구유닛 김재석
  2. 2. 책임연구원 2010/2011, 마비노기2 2006/2009, 마비노기 영웅전 테크니컬 디렉터 2004/2005, 마비노기 2003/2004, 프로젝트 T2[ɡ̊ɪm̚ ʨæːsɤk̚] /ɡim ɟɛːzʌɡ/ 2001/2003, Oz World
  3. 3. AgendaVisual Studio .NET Rainer .NET Framework 1.0 C# 1.0Visual Studio .NET 2003 Everett .NET Framework 1.1, C# 1.2Visual Studio 2005 Whidbey .NET Framework 2.0, C# 2,0 .NET Framework 3.0 WinFXVisual Studio 2008 Orcas .NET Framework 3.5, C# 3.0Visual Studio 2010 Dev10 .NET Framework 4.0, C# 4.0 Reactive Extensions for .NET Framework 3.5 SP1
  4. 4. VS .NET VS .NET 2003 VS 2005 VS 2008 Rx Visual Studio .NET Rainer .NET Framework 1.0 / C# 1.0
  5. 5. VS .NET VS .NET 2003 VS 2005 VS 2008 Rx C# 1.0 – Managed Code Common Type System Classes Structures Enumerations Interfaces Delegates Attributes DirectivesVisual Studio® .NET
  6. 6. VS .NET VS .NET 2003 VS 2005 VS 2008 RxCommon Type SystemPrimitive TypesFieldMethodPropertyEvent
  7. 7. VS .NET VS .NET 2003 VS 2005 VS 2008 RxPrimitive TypesByte BooleanInt16 CharInt32 DecimalInt64 IntPtrSingleDouble String
  8. 8. VS .NET VS .NET 2003 VS 2005 VS 2008 RxField// C#const int constant = 0;readonly int readOnlyField;// C++static const int constant = 0;const int readOnlyField;mutable int mutableField;
  9. 9. VS .NET VS .NET 2003 VS 2005 VS 2008 RxField (cont.)Field ≠ variabledefault(T) if not initialized• null if reference type• 0 if value type
  10. 10. VS .NET VS .NET 2003 VS 2005 VS 2008 RxMethodMember function with CV-qualifierclass Object{public: String ToString() const { return const_cast<Object*>(this) ->ToString(); } virtual String ToString();};
  11. 11. VS .NET VS .NET 2003 VS 2005 VS 2008 RxMethod (cont.)// C#void CallByRef(ref int value);bool TryGetValue(out int value);// C++void CallByRef(int& value);bool TryGetValue(/*out*/int& value);
  12. 12. VS .NET VS .NET 2003 VS 2005 VS 2008 RxPropertyGet Method/Set Method ≠ reference to type// C#variable = obj.PropertyName;obj.PropertyName = value// C++variable = obj->get_PropertyName();obj->setPropertyName(value);
  13. 13. VS .NET VS .NET 2003 VS 2005 VS 2008 RxProperty (cont.)Indexed property ≠ index operator []int this[int index]{ get { return array[index]; } set { array[index] = value; }}
  14. 14. VS .NET VS .NET 2003 VS 2005 VS 2008 RxProperty (cont.)Implementation: lazy evaluation classclass Property<typename T>{ operator T() const; Property& operator=(const T& value); Property& operator=(const Property& value);};
  15. 15. VS .NET VS .NET 2003 VS 2005 VS 2008 RxProperty (cont.)VC 7.1 or higher: property extended storage- class modifiervirtual TypeName get_PropertyName();virtual void set_PropertyName(const TypeName& value);__declspec(property(get=get_Property Name, put=set_PropertyName)) TypeName PropertyName;
  16. 16. VS .NET VS .NET 2003 VS 2005 VS 2008 RxEventMulticast delegate// C#obj.EventName += someDelegate;obj.EventName -= someDelegate;if (obj.EventName != null) obj.EventName();// C++obj->add_EventName(someDelegate);obj->remove_EventName(someDelegate);obj->raise_EventName();
  17. 17. VS .NET VS .NET 2003 VS 2005 VS 2008 RxClassesFinalizer ≠ destructorGC.ReRegisterForFinalize(this);GC.SuppressFinalize(this);// C#GC.KeepAlive(value);// C++Pointer<Object> keepAlive(value);
  18. 18. VS .NET VS .NET 2003 VS 2005 VS 2008 RxClasses (cont.)Prevent R6025 – Pure virtual function call__forceinline void Release(){ if (--referenceCounter == 0) { Finalize(); delete this; }}
  19. 19. VS .NET VS .NET 2003 VS 2005 VS 2008 RxClasses (cont.)Type initializer a.k.a. static constructornamespace{ class Static : … ; Pointer<Static> _static;}… if (_static == NULL) { _static.CompareExchange(Pointer<Static>( new Static), Pointer<Static>(NULL)); }
  20. 20. VS .NET VS .NET 2003 VS 2005 VS 2008 RxClasses (cont.)Sealed classtemplate <typename T>class Sealed{private: friend typename T; Sealed() { } ~Sealed() { }};
  21. 21. VS .NET VS .NET 2003 VS 2005 VS 2008 RxStructuresLightweight – Value typesDefault constructor / finalizer• NEVER let contain unmanaged resourcesExpert only• Box/Unbox cost
  22. 22. VS .NET VS .NET 2003 VS 2005 VS 2008 RxEnumerationsValue typesstring[] Enum.GetNames()TEnum[] Enum.GetValues()TEnum Enum.Parse(string s)bool Enum.TryParse( string s, out TEnum @enum)TEnum TEnum.MaxValue { get; }TEnum TEnum.MinValue { get; }
  23. 23. VS .NET VS .NET 2003 VS 2005 VS 2008 RxInterfacesVirtual inheritancetemplate <>class Task<void> : public Object, public virtual IAsyncResult, public virtual IDisposable{…};
  24. 24. VS .NET VS .NET 2003 VS 2005 VS 2008 RxDelegatesClosures or managed functorsCalling conventions:• cdecl• stdcall• fastcall• thiscall• function object
  25. 25. VS .NET VS .NET 2003 VS 2005 VS 2008 RxDelegates (cont.)#define DEFINE_DELEGATE(TPARAMS, PARAMS, ARGS)template <typename TResult TPARAMS>class delegate<TResult(PARAMS)> …#define DELEGATE_TPARAMS , typename T1#define DELEGATE_PARAMS T1 arg1#define DELEGATE_ARGS arg1DEFINE_DELEGATE(DELEGATE_TPARAMS, DELEGATE_PARAMS, DELEGATE_ARGS);
  26. 26. VS .NET VS .NET 2003 VS 2005 VS 2008 RxAttributestemplate <typename T>class CustomAttribute{public: static const int get_Value() { return value; }private: static int value;};#define CUSTOM_ATTRIBUTE(Type, _v) int CustomAttribute<Type>::value = _v
  27. 27. VS .NET VS .NET 2003 VS 2005 VS 2008 RxDirectivesusing directive (C#) ≈ using directive (C++)Assemblies are referenced. #include directive (C++) package / import declarations (Java)#define directive defines a symbol assigns a value to a symbol
  28. 28. VS .NET VS .NET 2003 VS 2005 VS 2008 Rx Visual Studio .NET 2003 Everett .NET Framework 1.1 / C# 1.2
  29. 29. VS .NET VS .NET 2003 VS 2005 VS 2008 Rx C# 1.2 – Bug Fix IDisposable interface • IEnumerator interface foreach (object obj in collection) // TEnumerator enumerator = // collection.GetEnumerator(); { } // if (enumerator is IDisposable) // enumerator.Dispose();Visual Studio® .NET 2003
  30. 30. VS .NET VS .NET 2003 VS 2005 VS 2008 Rx Visual Studio 2005 Whidbey .NET Framework 2.0 / C# 2.0
  31. 31. VS .NET VS .NET 2003 VS 2005 VS 2008 Rx C# 2.0 – Generic Generics Iterators Partial Classes Nullable Types Anonymous Methods Static Classes Property Accessor Accessibilities DelegatesVisual Studio® 2005
  32. 32. VS .NET VS .NET 2003 VS 2005 VS 2008 RxGenericsSystem.Collections.Generic• List<T> std::vector<T>• Queue<T> std::queue<T>• Stack<T> std::stack<T>• LinkedList<T> std::list<T>• Dictionary<TKey, TValue> std::map<Key, T>• HashSet<T> std::set<T>
  33. 33. VS .NET VS .NET 2003 VS 2005 VS 2008 RxIteratorsIEnumerable<T> Where<T>( IEnumerable<T> source, Func<T, bool> predicate){ if (source == null) yield break; foreach (T value in source) { if (!predicate(value)) continue; yield return value; }}
  34. 34. VS .NET VS .NET 2003 VS 2005 VS 2008 RxPartial Classes// Generated.Designer.cs[CLSCompliant]partial class Generated : ICloneable{ … }// Generated.cs[Serializable]partial class Generated : IDisposable{ … }
  35. 35. VS .NET VS .NET 2003 VS 2005 VS 2008 RxNullable TypesNullable<T> where T : structCoalescing operator ??int? nullable = null;int value = nullable ?? default(int);
  36. 36. VS .NET VS .NET 2003 VS 2005 VS 2008 RxAnonymous Methods// C# 2.0Func<Type, bool> predicate = // nested type in C# 1.2 delegate(Type value) { return value == default(Type); };
  37. 37. VS .NET VS .NET 2003 VS 2005 VS 2008 RxStatic ClassesClasses which are both abstract and sealed.public static class Helper{ public static int GetHash(object obj) { return obj.GetHashCode(); }}
  38. 38. VS .NET VS .NET 2003 VS 2005 VS 2008 RxProperty AccessorAccessibilities// C# 1.2public Type PropertyName{ get { return field; } }internal void SetPropertyName(Type value){ fieldName = value; }// C# 2.0public Type PropertyName{ get { return fieldName; } internal set { fieldName = value; }}
  39. 39. VS .NET VS .NET 2003 VS 2005 VS 2008 RxDelegatesstring Covariance() …Func<object> function = new Func<object>(Covariance);void Contravariance(object arg);Action<string> action = new Action<string>( Contravariance);
  40. 40. VS .NET VS .NET 2003 VS 2005 VS 2008 RxDelegates (cont.)// C# 1.2Action action = new Action(obj.MethodName);// C# 2.0Action action = obj.MethodName;
  41. 41. VS .NET VS .NET 2003 VS 2005 VS 2008 Rx Visual Studio 2008 Orcas .NET Framework 3.5 / C# 3.0
  42. 42. VS .NET VS .NET 2003 VS 2005 VS 2008 Rx C# 3.0 – LINQ Implicitly Type Local Variables and Arrays Object Initializers Collection Initializers Extension Methods Anonymous Types Lambda Expressions Query Keywords Auto-Implemented Properties Partial Method DefinitionsVisual Studio® 2008
  43. 43. VS .NET VS .NET 2003 VS 2005 VS 2008 RxImplicitly Type LocalVariables and Arrays// C# 2.0Dictionary<string, object> dict = new Dictionary<string, object>();Type[] types = new Type[] { typeof(int) };// C# 3.0var dict = new Dictionary<string, object>();var types = new[] { typeof(int) };
  44. 44. VS .NET VS .NET 2003 VS 2005 VS 2008 RxObject Initializers// C# 2.0SomeKeyValue pair = new SomeKeyValue();value.Name = “anonymous”;value.Value = null;// C# 3.0var value = new SomeKeyValue() { Name = “anonymous”, Value = null, };
  45. 45. VS .NET VS .NET 2003 VS 2005 VS 2008 RxCollection Initializers// C# 2.0List<int> list = new List<int>();list.Add(1);list.Add(2);// C# 3.0var list = new List<int> { 1, 2 };
  46. 46. VS .NET VS .NET 2003 VS 2005 VS 2008 RxExtension Methodsstatic class ComponentExtensions{ static void DoSomething( this IComponent component, T arg) { … }}ComponentExtensions.Do(component, arg);component.DoSomething(arg);
  47. 47. VS .NET VS .NET 2003 VS 2005 VS 2008 RxAnonymous Typesforeach (var value in from row in table select new { Type = row.GetType(), Hash = row.GetHashCode(), }){ Console.WriteLine(“{{ Type = {0}, Hash = {1} }}”, value.Type, value.Hash); Console.WriteLine(value);}
  48. 48. VS .NET VS .NET 2003 VS 2005 VS 2008 RxLambda Expressions// C# 2.0Func<Type, bool> predicate = delegate(Type value) { return value == default(Type); };// C# 3.0Func<Type, bool> predicate = value => value == default(Type);
  49. 49. VS .NET VS .NET 2003 VS 2005 VS 2008 RxQuery Keywordsvar q = from c in categories join p in products on c.ID equals p.CID orderby c.ID, p.Price descending group p.Price by c.ID into g let cid = g.Key where Predicate(cid) select g.Average();
  50. 50. VS .NET VS .NET 2003 VS 2005 VS 2008 RxQuery Keywords (cont.)var q = categories .Join(products, c => c.ID, p => p.CID, (c, p) => new { c = c, p = p }) .OrderBy(_ => _.c.ID) .ThenByDescending(_ => _.p.Price) .GroupBy(_ => _.c.ID, _.p.Price) .Select(g => new { g = g, cid = g.Key }) .Where(_ => Predicate(_.cid)) .Select(_ => _.g.Average());
  51. 51. VS .NET VS .NET 2003 VS 2005 VS 2008 RxAuto-ImplementedProperties// C# 2.0public Type PropertyName{ get { return fieldName; } internal set { fieldName = value; }}// C# 3.0public Type PropertyName{ get; internal set; }
  52. 52. VS .NET VS .NET 2003 VS 2005 VS 2008 RxPartial MethodDefinitions// Generated.Designer.cspartial class Generated{ Generated() { OnInitialized(); } partial void OnInitialized();}// Generated.cspartial class Generated{ partial void OnInitialized() { … }}
  53. 53. VS .NET VS .NET 2003 VS 2005 VS 2008 Rx More LINQ with System.Interactive – Getting Started by Bart De Smet Reactive Extensions for .NET Framework 3.5 SP1
  54. 54. VS .NET VS .NET 2003 VS 2005 VS 2008 RxReactive ExtensionsTask Parallel Library ∈ .NET Framework 4.0Observer Design Pattern
  55. 55. VS .NET VS .NET 2003 VS 2005 VS 2008 RxTask Parallel LibrarySystem.Collections.Concurrent• ConcurrentBag<T> tbb::concurrent_vector<T>• ConcurrentQueue<T> tbb::concurrent_queue<T>• ConcurrentStack<T> tbb::concurrent_stack<T>• ConcurrentDictionary<TKey, TValue> tbb::concurrent_hash_map<Key, T>
  56. 56. VS .NET VS .NET 2003 VS 2005 VS 2008 RxTask Parallel Library(cont.)System.Threading.Tasks// C# 3.0ThreadPool.QueueUserWorkItem( callback, state);// C# 4.0 or RxTask.Factory.StartNew( action, state);
  57. 57. VS .NET VS .NET 2003 VS 2005 VS 2008 RxObserver Design Pattern More LINQ with System.Interactive – Getting Started by Bart De Smet
  58. 58. VS .NET VS .NET 2003 VS 2005 VS 2008 RxObserver Design Pattern(Further reading)Duality of IEnumerable<T> / IObservable<T>Reactive Framework Finally ExplainedMore LINQ with System.Interactive series
  59. 59. Q&Atwitter:@tcaesvk

×