Tuesday, July 14, 2009

In programming (C#), why are some methods invoked different ways than others?

Example:


To invoke an array sort method, it is written


Class.Method(Instance);


or


Array.Sort(array1);





But to invoke a string split method, it is written


Instance.Method();


or


string1.Split(' ');





Why are they different and what is the key deciding factor that differentiates between the two? In other words, how will I know to write it one way or the other while coding other than trial and error?





Thanks!

In programming (C#), why are some methods invoked different ways than others?
I'm not sure about C#, but it's very similar to Java. So, they SHOULD work the same way.





//When you declare a static method, you will need to do it this way. The method will not refer to any instance variable.


//In this example, you are calling the Array class to sort array1. The Sort method dont need to refer to any instance variable to sort the array. It can just read the elements and sort it up and then return the result back to you.


Class.Method(Instance);


or


Array.Sort(array1);





//This is a non-static method. So the method is dependent on the instance variable.


//Like for this example, you are splitting the string1 variable itself.


But to invoke a string split method, it is written


Instance.Method();


or


string1.Split(' ');





You just need to understand the concept of static vs non-static methods. Do you think an Add() method needs to be static or not? Does the Add() method need to refer to any instance variable? No! All it needs is numerical values to be passed in the method then it'll just add them up and return the result. No reference to an instance variable needed.





Hope that'll help you understand the difference.
Reply:These methods are called differently because of the way they are declared in the class. Array.Sort is a static method, whereas string1.Split is a regular method. Static methods belong to the class, and are not dependent on any instance of the object to function (unless they take an instance of the object as a parameter). In other words, Array.Sort can be called even if you have not created any Array objects. Of course, if you have no Array objects to be sorted, how can you call Array.Sort when it takes an Array object as a parameter? (You can't, but the point is its AVAILABLE to be called)





So, why are some methods static (Array.Sort) and other methods are regular (string1.Split)? It's mainly a matter of practicality. Only one copy of a static method is used, no matter how many objects exist, while a copy of a regular method is created each time an object is instantiated. This can reduce the size of the compiled program dramatically, especially if there are arrays of objects. For example, consider a two-dimensional array: an Array object whose elements are all Array objects. Another reason to use static methods is style/code organization. Look at the Math class. Its a way to group all those functions together, even though you never create a Math object (infact, you cannot create a Math object).





As for knowing how to use all these methods, it is somewhat frustrating at first. You will quickly learn the commonly-used classes just from routine use; Intellisense helps me tremendously with this. I'm certain I will never know ALL of the .Net classes. I know the classes I use frequently, and when I need to use a class I'm not familiar with, I simply look it up in Visual Studio help, or go to msdn.





Hope this has been helpful, and hang in there, you'll spend less time on msdn looking up classes as you do more programming.


No comments:

Post a Comment