FileInfo[] = dir.GetFile();

Dom Fux

New member
Joined
May 23, 2017
Messages
2
Location
Paris (suburb)
Programming Experience
Beginner
Hello all,

A question from a newbie regarding c#

Why an array of FileInfo objects does not need a "new" in order to allocate space and I am unable to find any" new FileInfo"out of the small programI am studying???

The line is FileInfo[] files = dir.Getfile(); //dir is from DirectoryInfo type and has been allocated before with a "new" like DirectoryInfo dir = new DirectoryInfo(path);

I know we dont know how many filename we will get ....
I am lost thanks in advance
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,196
Location
Sydney, Australia
Programming Experience
10+
You only need to use the 'new' keyword to invoke a constructor if YOU are creating an object. In this YOU are not creating an object. You are calling DirectoryInfo.GetFiles and IT is creating the object. That means that there would be code inside that GetFiles method that does use the 'new" keyword to create an object. It's pretty simple really. You just need to ask yourself whether you are creating an object or whether you're using one that was created elsewhere.
 
Top Bottom