As you may know Microsoft .Net itself is a framework and it executes lots of checks in the backgroud to make use of the the best available resources in your computer system as well as to manage the Microsoft .Net code execution in a smooth way. So it is really needed that one should know a bit about how your Microsoft .Net code executes in .net CLR (common language runtime) managed environment. A few about the Microsoft .Net type casting scenarios and what actually CLR does during type casting are here. You can increase your Microsoft .Net C# code execution performance if you really concentrate on the facts like this.
Many of us used to check the incoming type of an object and normally we go for calling Getype method or using is keyword like
if ( obj is DateTime)
{
dt = (DateTime)obj //or
if ( typeof(obj) == DateTime.GetType())
{
dt = (DateTime)obj
}
}
if you are using C# and your motive here is to just avoid an InvalidCastException and use the object which is inside obj, then i would say that this code will affect your performance and we have better ways to code in Microsoft .Net C# which gains us application performance. Here .net will check the incoming object's type details and metadata (probably by using reflection) two times. one within if and other while you are casting the object to DateTime. So consider the code below.
dt = obj as DateTime /// if the type is not matching this wont throw an InvalidCastException. but just returns null
if (null != dt) /// this ensures that an object reference is assigned to dt.
{ //use the object dt here for whatever you want }
This way .net framework clr will check for obj's type details and metadata only once..
Another aspect which we should take care of is about the parameters passed to a method. This has more about to do with a design aspect rather than performance.
Say you have a utility method called Manipulate and for now it accepts a parameter of type LIST or hash table, rather than creating a method like
Public void Manipulate(List<string> lst)
{
}
//Use
Public void Manipulate(IEnumerable<T> lst)
{
}
In this way in future you will be able to reuse the same method code if it is required for any other objects like Hashtable or dictionary provided that you maintained that genericity in your method implementaion too.
Next let us take a topic on garbage collection.
Say that you have a class called Student with a method in it called Find
Public class Student {
Public Student Find(string Name) { // code here }
}
And your main calling method will look like
Student stud = new Student();
stud = stud .Find(“John”) or Student st = Stud.Find(“John”)
// Considering the second one the prior method is better since we are using the same object
But there also we have a catch. According to the method implementaion Find accepts just one parameter which is string and the return type is of Student. So obviously inside Find we need to create a new instance of Student so that we can return it to the caller.
Here what happens is, the statement stud = stud .Find(“John”) will removes the object referance which is already there in stud and replaces it with the one returned from Find method.
Now the intial object referance which got created as a result of the statement = new Student() is left loose and now is a candidate for garbage collection and this part of memmory cannot be used utill GC reclaims that memmory. Also it is not advisable to initiate GC.Collect() since after every GC a memmory compaction is made
This post was useful ?. Let us know your comments.. questions or doubts if any..
Was this post helpful to you ? !!.. Then please like this in any of the social media below !.
Let it be useful for others too..