CSharp

From Wiki

Jump to: navigation, search

C#

Performance Tips

  • Use value type(System.ValueType, for ex. int, bool...) instead of reference type (System.Object, for ex. class, interface)

Value type will be store in the Stack while Reference Type is in Heap

  • Declare the value first before go into the loop
int lenght = myArray.Lenght; 
for(int x = 0; x < lenght; x++) 
{ 
   //do something...
}
  • Minimize use of Throw Exception because it expensive
  • Avoid Manual Garbage Collection

Class vs Struct

Struct – used for things without an obvious identity. When modelling the number 3 then a struct makes sense. There can be lots of 3's in the world and we don't want to share a single 3 across multiple uses. So we generally want values to clone themselves easilly.

Class – used for things with an obvious identity. For example, we only want one instance of a WhirlpoolMember class with name "Xception" because it models that single person. We don't want this object to clone itself easilly so passing around references matches our use.


  • Struct is value type and Class is reference type.
  • Instance of reference type get allocated in the managed heap and are gabbarge collected when there is no outstanding
    reference to them.
  • Instance of value type are located in stack. allocated memory are reclaimed as soon as their scope ends.
  • Java don't have struct (value type), so you can define only reference type.
  • Value type get pass by value, and reference type by reference. If value type are pass as a parameter in the function, it will copy the data so that the original data will not change even we change it inside the function.
  • In C#, 2 way to create value type.
  1. Using struct
  2. Using enum


When to use structs

  • You want your type to look and feel like a primitive type. Like "int, float, char, DateTime, TimeSpan".
  • You create a lot of instances, use them briefly, and then drop them. For e.g., within a loop.
  • The instances you create are not passed around a lot.
  • You don't want to derive from other types or let others derive from your type.
  • You want others to operate on a copy of your data (basically pass by value semantics).


Here's when not to use structs:

  • The size of the struct (the sum of the sizes of its members) gets large. The reason is that beyond a particular size, the overhead involved in passing it around gets prohibitive.

Microsoft recommends that the size of a struct should ideally be below 16 bytes for struct, but it really is up to you. In case your struct has reference types as members, make sure you don't include the size of instances of reference types, just the size of the references.
* You create instances, 'put them in a collection, 'iterate and modify elements in the collection. This will result in a lot of boxing/unboxing as FCL Collections operate on System.Object. Every addition will involve a boxing operation, and every modification will involve an unboxing followed by a boxing operation.