10 C# tips in 140 characters

Today I have gone on a rampage on C# on my Twitter account, which I would like to share with you here. Follow the hashtag #csharptip so that you do not miss any. I would say it is never an easy job to share programming tips in 140 characters. Hence, I have tried hard to squeeze as much information as into those tweets. I hope you will read it after decompressing. Smile

  • Tip 1: double’s internal representation is base 2 and native to the processor, decimal’s base 10, which is 10 times slower than double
  • Tip 2: double is sufficient for most scientific calculations, whereas decimal is more appropriate for financial.
  • Tip 3: dont digest, catch & throw that arithmetic overflow: var min = int.MinValue; checked { –min; }
  • Tip 4: unlike const, readonly objects you can assign in two places: declaration time as well as inside the constructor
  • Tip 5: you do not need to upcast Animal = (Animal)tiger, but downcast explicitly: Animal animal = tiger; tiger = (Tiger)animal;
  • Tip 6: how do you make internal members visible? adding this: [assembly: InternalsVisibleTo ("Ally")]
  • Tip 7: you can extend another interface with the same method signatures: ICredit : IDebit
  • Tip 8: to implement the same methods of extended interfaces, do it explicitly: ICredit.Charge(decimal d) and IDebit.Charge(decimal d)
  • Tip 9: you can add implementation specific method body without virtual-override, using new: public new int GetHashCode()
  • Tip 10: use default(T) for generic type initialization and comparison for you do not know if it is going to be reference/value types

Again, do not forget to follow me on Twitter: TanzimSaqib

Comments

4 thoughts on “10 C# tips in 140 characters

  1. Pingback: DotNetShoutout

  2. Pingback: 10 C# tips in 140 characters Part-2

  3. Pingback: 20 C# tips in 140 characters « turtle9

  4. Pingback: Weekly Link Post 189 « Rhonda Tipton's WebLog

Leave a Reply