Hi all,
I'm a newbie in C#, appreciated if someone can clear my doubt on the class & type:
DirectoryInfo is a .net class, we need to instantiate an object to use it as below:
string path = @"C:\My Folder";
DirectoryInfo di = new DirectoryInfo(path);
However, why we also can use this class as a type as shown below:
DirectoryInfo parent = Directory.GetParent(path);
Loading
VulpesPosted May 6, 2011, 5:05 AM
As you gain more experience of C#, this sort of thinking will become automatic :)
Kok Keong ChuaPosted May 9, 2011, 10:53 PM
Sam HobbsPosted May 6, 2011, 8:33 PM
One way to determine that Directory.GetParent is static is to go to the MSDN and search for it, as in:
http://social.msdn.microsoft.com/Search/en-US?query=directory.getparent&ac=1
Then click on the Directory.GetParent Method (System.IO) . See where it says that it is static? Another way to get to the documentation is to put the cursor over the method or property and press F1.
Ankit NandekarPosted May 6, 2011, 1:06 AM
in ur above example string is also a class but it is inbuild class provided by visual stdio,to check this weather string is a class or not in ur program right click on string and u can see Go to definition option is there click on it will show u how string class implemented in visual stdio.when we r writing like string path="c:/My Folder"; it is automaticly create instance of string class.
We r creating instance of a class because class is a type and type not taking any memory space in memory to use class members we have to create instance which is taking memory space in memory for example
int 5;//error because we can not store directly value into types
int i=5;//work
int is a value type so it is define as a structure in vs,all value types define as a structure in vs and all reference type like string define as a class in visual studio.