2. » Desktops
˃ WinForm
˃ WPF
˃ Linux, Mac, Solaris
» Servers
˃ Web Apps
˃ Services (web/native)
» Embedded Systems
˃ Micro .NET (Netduino, FEZ Domino)
˃ Windows Phone
˃ Robots
˃ iPhone
» Gaming Consoles
˃ Xbox 360
˃ Wii, Playstation 3 (via Mono)
2
25 Things I've Learned about C# - Phil Denoncourt
http://mono-project.com
3. » C# compiles to bytecode
˃ So it must be slow
» Bytecode gets compiled to native code upon
first usage
˃ Takes advantage of features that machine’s processor
» Matches C++ in Int math, trig, and I/O.
˃ Little slower in floating point
» C# benefits from disposing of objects when idle
˃ rather than when object is no longer used.
» http://www.osnews.com/story/5602&page=3
3
25 Things I've Learned about C# - Phil Denoncourt
4. » Built into the framework since 3.0
˃ Add reference to System.Speech
» Recognition
˃ Command based
˃ Dictation
» Synthesis
» Requires SAPI
˃ Windows Vista and Later
˃ Office 2003 and Later
» http://gotspeech.net
4
25 Things I've Learned about C# - Phil Denoncourt
5. » It’s easy to write
˃ ThreadPool.QueueUserWorkItem
˃ PLINQ
˃ BackgroundWorker component
» It’s hard to debug
˃ Add trace / logging to your code
» Make sure your classes are “ThreadSafe”
˃ Lock keyword
˃ Interlocked class
» http://www.bluebytesoftware.com/blog/
5
25 Things I've Learned about C# - Phil Denoncourt
6. » Added in 3.0. Implemented as a generic class
˃ Allows you to consider value types that are nullable
» Add ? to end of type
˃ Int?
˃ DateTime?
˃ String is already a reference type, thus already nullable
» Null Coalesce operator
˃ Substitutes a value if the object is null
˃ Really low in operator precedence. Often need parens.
» http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx
6
25 Things I've Learned about C# - Phil Denoncourt
7. » Exceptions hold a lot of information
» Throwing them is computationally expensive
˃ Avoid using exceptions as logic flow
» Stack trace line numbers are available if PDBs
are present with the binaries
» There is a difference between
˃ throw
˃ throw ex;
» http://msdn.microsoft.com/en-
us/library/5b2yeyab(v=VS.100).aspx
7
25 Things I've Learned about C# - Phil Denoncourt
8. » checked
˃ By default, overflow checking is disabled in C#
+ Depending on compiler options
˃ Placing statements in checked block enables checking
˃ There is also an unchecked keyword
˃ http://msdn.microsoft.com/en-us/library/74b4xzyw(v=VS.100).aspx
» volatile
˃ Keyword that indicates field could be updated by multiple threads
+ Disables compiler optimizations that assume single threaded access
˃ Places a “lock” statement around access to the field
˃ http://msdn.microsoft.com/en-us/library/x13ttww7(v=VS.100).aspx
8
25 Things I've Learned about C# - Phil Denoncourt
9. » Allows you to add new methods to existing classes
˃ Even sealed classes
» Can operate on public members
˃ But not private members
» Good way to add “missing” functionality to class
˃ Good way to make something unmaintainable
» Internally – implemented as a static method
passing in the current instance.
» http://extensionmethod.net/
9
25 Things I've Learned about C# - Phil Denoncourt
10. » Generic template that defers initialization until
used
» System.Lazy<T>
» Object will be initialized when the value
member is accessed, or ToString() is called.
» http://weblogs.asp.net/gunnarpeipman/archive/2009/05/19/net-framework-4-0-using-system-lazy-lt-t-gt.aspx
10
25 Things I've Learned about C# - Phil Denoncourt
11. » Lambda operator =>
» Anonymous function
˃ Contains code
» Returns a delegate
» Used extensively in Linq
» http://msdn.microsoft.com/en-
us/library/bb311046.aspx
11
25 Things I've Learned about C# - Phil Denoncourt
12. » Eliminates a lot of repetitive coding
» Implemented as extension methods in
System.Linq.dll
» Must reference the assembly, and add a using
statement:
˃ using System.Linq;
» Different providers allow access to different types
of data by exposing IQueryable interface
˃ Linq to Objects
˃ Linq to SQL
˃ Linq to Entities
˃ Linq to Xml
» http://msdn.microsoft.com/en-us/netframework/aa904594.aspx
12
25 Things I've Learned about C# - Phil Denoncourt
13. » Normally static fields exist once per AppDomain
» [ThreadStatic] static fields exist once per Thread
» Static constructors and initializers still only
execute once.
˃ So you have to make sure the object is not null before using it
» http://blogs.msdn.com/b/jfoscoding/archive/2006/07/18/670497.
aspx
13
25 Things I've Learned about C# - Phil Denoncourt
14. » Allows you to move a class from one assembly
to another without breaking callers
» [assembly:TypeForwardedTo(typeof(classThatWasMoved)]
˃ Place this statement in the original assembly, which must reference
the new assembly.
» Used for refactoring types into different
assemblies, and not have to rebuild the caller
assembly.
˃ Namespace and name can’t change
» Can also redirect entire assembly in .config file
˃ assemblyBinding section
» http://msdn.microsoft.com/en-us/library/ms404275.aspx
14
25 Things I've Learned about C# - Phil Denoncourt
15. » Don’t need to be based on int
˃ Can be byte, sbyte, short, ushort, int, uint, long, or ulong.
» If you use the FlagsAttribute, you can perform
bitwise operations
˃ Meaning that you can store more than one state in an enum variable.
» http://dotnetperls.com/enum-flags
15
25 Things I've Learned about C# - Phil Denoncourt
16. » -In addition to allowing shortcuts to
namespaces
» Automatically disposes objects when scope is
outside of the using statement.
» Object doesn’t need to be instantiated in the
using statement
˃ But should
» More than one object can be tracked by a using
statement
˃ But they must be of the same type
» http://msdn.microsoft.com/en-us/library/yh598w02.aspx
16
25 Things I've Learned about C# - Phil Denoncourt
17. » Allows you to examine metadata about
compiled code.
» Has a reputation for being slow
˃ I haven’t found that to be very true
» Allows access to private methods / fields of a
class
» You can also get the actual IL of the method for
reverse engineering
˃ Which is what Reflector does
» http://msdn.microsoft.com/en-us/library/f7ykdhsy(v=VS.100).aspx
17
25 Things I've Learned about C# - Phil Denoncourt
18. » Can call other constructor methods
˃ Add :this to call a constructor in the current object
˃ Add :base to call a constructor in the base object
» http://msdn.microsoft.com/en-us/library/ms173115.aspx
18
25 Things I've Learned about C# - Phil Denoncourt
19. » Allows you to write code against an abstract type
˃ Type Parameter
» When generic is used, a type is specified.
˃ Reduces the amount of boxing
» Generics can be applied to
˃ Classes
˃ Delegates
˃ Methods
˃ Interfaces
» Instead of comparing to null, compare to default(t)
» http://msdn.microsoft.com/en-
us/library/512aeb7t(v=VS.100).aspx
19
25 Things I've Learned about C# - Phil Denoncourt
20. » Allows you to constrain what types can be used
with a generic
» Can constrain
˃ Classes that implement an interface
˃ Classes must have a common base class
˃ Parameterless constructor
˃ That the type is a class
˃ That the type is a struct
» Constraints enforced by the compiler
» http://msdn.microsoft.com/en-us/library/d5x73970.aspx
20
25 Things I've Learned about C# - Phil Denoncourt
21. » Very easy since .NET 2.0
˃ No more worrying about holding onto disposable objects
» File class – static methods for
˃ Copying
˃ Renaming
˃ Deleting
˃ Checking existing
˃ Reading /writing data to a file
˃ Encrypt/Decrypt a file
˃ http://msdn.microsoft.com/en-us/library/system.io.file.aspx
» Path class – static methods for
˃ Assembling a filename
˃ Changing extensions
˃ Getting a truly temporary filename
˃ http://msdn.microsoft.com/en-us/library/system.io.path.aspx
21
25 Things I've Learned about C# - Phil Denoncourt
22. » Mostly done for you
» .NET 4.0 supports 354 different cultures
» Allows you to properly
˃ Format numbers
˃ Format currency
˃ Format date/times
˃ Compare strings
» By default, the culture of the current thread is used
when formatting/comparing
» Globalization != Localization
» http://en.wikibooks.org/wiki/.NET_Development_Foundation/Glo
balization
22
25 Things I've Learned about C# - Phil Denoncourt
23. » You can write code that writes code
» Emits C# or VB.Net
» Or can build an assembly using a compiler
» Can build the assembly in memory
˃ Doesn’t get written to disk.
» http://www.15seconds.com/issue/020917.htm
23
25 Things I've Learned about C# - Phil Denoncourt
24. » GZipStream class only compresses one file
» System.IO.Packaging
˃ Add reference to “WindowsBase”
» Add each file as part of the package
» It will add a [Content_Types].xml file to every
zip file
˃ Part of the open packaging specification
» http://madprops.org/blog/zip-your-streams-with-system-io-
packaging/
24
25 Things I've Learned about C# - Phil Denoncourt
25. » Goto in switch statement
» >> multiplies by powers of 2, << divides
˃ Real fast
» as operator
˃ Faster than cast, doesn’t throw exception
» @ string constant qualifier
˃ Ignores escapes
» StringBuilder concatenates strings faster
˃ For only for large amounts of data
25
25 Things I've Learned about C# - Phil Denoncourt
26. » Code Access Security
» Strong names
» Lackluster support for Xml Documentation
» Lack of development in certain libraries
˃ Linq to SQL
˃ ASP.NET
26
25 Things I've Learned about C# - Phil Denoncourt