NDC 2011, C++ 프로그래머를 위한 C#

644 views

Published on

2011-06-01

Published in: Technology, Sports
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
644
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

NDC 2011, C++ 프로그래머를 위한 C#

  1. 1. C++ 프로그래머를 위한 C#㈜넥슨신규개발3본부 개발1실 GTR팀 성능연구유닛김재석
  2. 2. 책임연구원2010/2011, 마비노기22006/2009, 마비노기 영웅전테크니컬 디렉터2004/2005, 마비노기2003/2004, 프로젝트 T22001/2003, Oz World[ɡ̊ɪm̚ ʨæːsɤk̚]/ɡim ɟɛːzʌɡ/
  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.0Reactive Extensions for .NET Framework 3.5 SP1
  4. 4. Visual Studio .NET Rainer.NET Framework 1.0 / C# 1.0VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  5. 5. C# 1.0 – Managed CodeCommon Type SystemClassesStructuresEnumerationsInterfacesDelegatesAttributesDirectivesVisual Studio® .NETVS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  6. 6. Common Type SystemPrimitive TypesFieldMethodPropertyEventVS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  7. 7. Primitive TypesByteInt16Int32Int64SingleDoubleBooleanCharDecimalIntPtrStringVS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  8. 8. Field// C#const int constant = 0;readonly int readOnlyField;// C++static const int constant = 0;const int readOnlyField;mutable int mutableField;VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  9. 9. Field (cont.)Field ≠ variabledefault(T) if not initialized• null if reference type• 0 if value typeVS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  10. 10. MethodMember function with CV-qualifierclass Object{public:String ToString() const{return const_cast<Object*>(this)->ToString();}virtual String ToString();};VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  11. 11. Method (cont.)// C#void CallByRef(ref int value);bool TryGetValue(out int value);// C++void CallByRef(int& value);bool TryGetValue(/*out*/int& value);VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  12. 12. PropertyGet Method/Set Method ≠ reference to type// C#variable = obj.PropertyName;obj.PropertyName = value// C++variable = obj->get_PropertyName();obj->setPropertyName(value);VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  13. 13. Property (cont.)Indexed property ≠ index operator []int this[int index]{get { return array[index]; }set { array[index] = value; }}VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  14. 14. Property (cont.)Implementation: lazy evaluation classclass Property<typename T>{operator T() const;Property& operator=(const T&value);Property& operator=(constProperty& value);};VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  15. 15. Property (cont.)VC 7.1 or higher: property extended storage-class modifiervirtual TypeName get_PropertyName();virtual void set_PropertyName(constTypeName& value);__declspec(property(get=get_PropertyName, put=set_PropertyName))TypeName PropertyName;VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  16. 16. EventMulticast 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();VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  17. 17. ClassesFinalizer ≠ destructorGC.ReRegisterForFinalize(this);GC.SuppressFinalize(this);// C#GC.KeepAlive(value);// C++Pointer<Object> keepAlive(value);VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  18. 18. Classes (cont.)Prevent R6025 – Pure virtual function call__forceinline void Release(){if (--referenceCounter == 0){Finalize();delete this;}}VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  19. 19. Classes (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));}VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  20. 20. Classes (cont.)Sealed classtemplate <typename T>class Sealed{private:friend typename T;Sealed() { }~Sealed() { }};VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  21. 21. StructuresLightweight – Value typesDefault constructor / finalizer• NEVER let contain unmanaged resourcesExpert only• Box/Unbox costVS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  22. 22. EnumerationsValue 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; }VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  23. 23. InterfacesVirtual inheritancetemplate <>class Task<void> : public Object,public virtual IAsyncResult,public virtual IDisposable{…};VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  24. 24. DelegatesClosures or managed functorsCalling conventions:• cdecl• stdcall• fastcall• thiscall• function objectVS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  25. 25. Delegates (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);VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  26. 26. Attributestemplate <typename T>class CustomAttribute{public:static const int get_Value() { returnvalue; }private:static int value;};#define CUSTOM_ATTRIBUTE(Type, _v) int CustomAttribute<Type>::value = _vVS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  27. 27. Directivesusing directive (C#) ≈ using directive (C++)Assemblies are referenced.#include directive (C++)package / import declarations (Java)#define directivedefines a symbolassigns a value to a symbolVS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  28. 28. Visual Studio .NET 2003 Everett.NET Framework 1.1 / C# 1.2VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  29. 29. C# 1.2 – Bug FixIDisposable interface• IEnumerator interfaceforeach (object obj in collection)// TEnumerator enumerator =// collection.GetEnumerator();{}// if (enumerator is IDisposable)// enumerator.Dispose();Visual Studio® .NET 2003VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  30. 30. Visual Studio 2005 Whidbey.NET Framework 2.0 / C# 2.0VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  31. 31. C# 2.0 – GenericGenericsIteratorsPartial ClassesNullable TypesAnonymous MethodsStatic ClassesProperty Accessor AccessibilitiesDelegatesVS .NET VS .NET 2003 VS 2005 VS 2008 RxVisual Studio® 2005
  32. 32. GenericsSystem.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>VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  33. 33. IteratorsIEnumerable<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;}}VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  34. 34. Partial Classes// Generated.Designer.cs[CLSCompliant]partial class Generated : ICloneable{ … }// Generated.cs[Serializable]partial class Generated : IDisposable{ … }VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  35. 35. Nullable TypesNullable<T> where T : structCoalescing operator ??int? nullable = null;int value = nullable ??default(int);VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  36. 36. Anonymous Methods// C# 2.0Func<Type, bool> predicate =// nested type in C# 1.2delegate(Type value){return value == default(Type);};VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  37. 37. Static ClassesClasses which are both abstract and sealed.public static class Helper{public static int GetHash(object obj){return obj.GetHashCode();}}VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  38. 38. Property 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; }}VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  39. 39. Delegatesstring Covariance() …Func<object> function =new Func<object>(Covariance);void Contravariance(object arg);Action<string> action =new Action<string>(Contravariance);VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  40. 40. Delegates (cont.)// C# 1.2Action action =new Action(obj.MethodName);// C# 2.0Action action = obj.MethodName;VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  41. 41. Visual Studio 2008 Orcas.NET Framework 3.5 / C# 3.0VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  42. 42. C# 3.0 – LINQImplicitly Type Local Variables and ArraysObject InitializersCollection InitializersExtension MethodsAnonymous TypesLambda ExpressionsQuery KeywordsAuto-Implemented PropertiesPartial Method DefinitionsVS .NET VS .NET 2003 VS 2005 VS 2008 RxVisual Studio® 2008
  43. 43. Implicitly 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) };VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  44. 44. Object Initializers// C# 2.0SomeKeyValue pair = new SomeKeyValue();value.Name = “anonymous”;value.Value = null;// C# 3.0var value = new SomeKeyValue(){Name = “anonymous”,Value = null,};VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  45. 45. Collection Initializers// C# 2.0List<int> list = new List<int>();list.Add(1);list.Add(2);// C# 3.0var list = new List<int> { 1, 2 };VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  46. 46. Extension Methodsstatic class ComponentExtensions{static void DoSomething(this IComponent component,T arg) { … }}ComponentExtensions.Do(component, arg);component.DoSomething(arg);VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  47. 47. Anonymous Typesforeach (var value in from row in tableselect new{Type = row.GetType(),Hash = row.GetHashCode(),}){Console.WriteLine(“{{ Type = {0}, Hash = {1} }}”,value.Type, value.Hash);Console.WriteLine(value);}VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  48. 48. Lambda Expressions// C# 2.0Func<Type, bool> predicate =delegate(Type value){return value == default(Type);};// C# 3.0Func<Type, bool> predicate =value => value == default(Type);VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  49. 49. Query Keywordsvar q = from c in categoriesjoin p in productson c.ID equals p.CIDorderby c.ID,p.Price descendinggroup p.Price by c.ID into glet cid = g.Keywhere Predicate(cid)select g.Average();VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  50. 50. Query 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());VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  51. 51. Auto-ImplementedProperties// C# 2.0public Type PropertyName{get { return fieldName; }internal set { fieldName = value; }}// C# 3.0public Type PropertyName{ get; internal set; }VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  52. 52. Partial MethodDefinitions// Generated.Designer.cspartial class Generated{Generated() { OnInitialized(); }partial void OnInitialized();}// Generated.cspartial class Generated{partial void OnInitialized() { … }}VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  53. 53. Reactive Extensions for .NET Framework 3.5 SP1VS .NET VS .NET 2003 VS 2005 VS 2008 RxMore LINQ with System.Interactive – Getting Started by Bart De Smet
  54. 54. Reactive ExtensionsTask Parallel Library ∈ .NET Framework 4.0Observer Design PatternVS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  55. 55. Task 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>VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  56. 56. Task Parallel Library(cont.)System.Threading.Tasks// C# 3.0ThreadPool.QueueUserWorkItem(callback, state);// C# 4.0 or RxTask.Factory.StartNew(action, state);VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  57. 57. Observer Design PatternVS .NET VS .NET 2003 VS 2005 VS 2008 RxMore LINQ with System.Interactive – Getting Started by Bart De Smet
  58. 58. Observer Design Pattern(Further reading)Duality of IEnumerable<T> / IObservable<T>Reactive Framework Finally ExplainedMore LINQ with System.Interactive seriesVS .NET VS .NET 2003 VS 2005 VS 2008 Rx
  59. 59. Q&A

×