TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Teemee Tang
NA
111
10.4k
C# Generics Help me please
Sep 13 2017 10:52 AM
Hi Guys,
Will you help me on my concern? I want to perform code reuse for the snippet below using Generics. How can I turn this to a single method? I am still not that familiar with Generics.
public void ReadInputFiles(string sourcePath)
{
CompanyAddressInputFile path = new CompanyAddressInputFile();
path.Path = sourcePath;
string[] filePaths = GetFiles<string>(path.Path);
foreach (string file in filePaths)
{
CompanyAddressInputFile companyAddressInputFile = new CompanyAddressInputFile();
companyAddressInputFile.Path = sourcePath;
companyAddressInputFile.Filename = Path.GetFileName(file);
ProcessFile(companyAddressInputFile);
}
}
public void ReadOutputFiles(string desPath)
{
CompanyAddressOutputFile path = new CompanyAddressOutputFile();
path.Path = desPath;
string[] filePaths = GetFiles<string>(path.Path);
foreach (string file in filePaths)
{
CompanyAddressOutputFile companyAddressOutputFile = new CompanyAddressOutputFile();
companyAddressOutputFile.Path = desPath;
companyAddressOutputFile.Filename = Path.GetFileName(file);
UpdateAddress(companyAddressOutputFile);
}
}
And, is below a good Generics method implementation?
public string[] GetFiles<T>(T filePath)
{
string[] filePaths = Directory.GetFiles(filePath.ToString());
return filePaths;
}
Reply
Answers (
5
)
downloading empty data template and upload to sql database
solve linear and quadratic style equations in C#