The Most Useful .NET Utility Classes Developers Tend To Reinvent Rather Than Reuse

Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Onaylı Üye
Katılım
7 Şub 2020
Mesajlar
112
Tepki puanı
6
Ödüller
2
Yaş
37
6 HİZMET YILI
Bağlantıları görmek için lütfen Giriş Yap

How often do you see code like this to create a file path?
C#:
public string GetFullPath(string fileName)
{
  string folder = ConfigurationManager.AppSettings["somefolder"];
  return folder + fileName;
}


Code like this drives me crazy because it is so prone to error. For example, when you set the folder setting, you have to remember to make sure it ends with a slash. Having too many things to remember makes this setup fragile.
Sure, you write some code to ensure that the folder has an ending slash, but I’d rather let someone write that code. For example, Microsoft.
The .NET framework is definitely huge so it can be understandable to miss out on some of the useful utility classes in there that will make your life as a developer easier.

C#:
public string GetFullPath(string filename)
{
  string folder = ConfigurationManager.AppSettings["somefolder"];
  return System.IO.Path.Combine(folder, filename);
}


The Path class is certainly well known and probably well used, but is still one of those classes that developers seem to never use to its full potential. For example, how often do you see this?
//make sure folder path ends with slash
C#:
string folder = GetFolderPath() + @"\";

Well that’s nice for Windows machines, but our world is changing and someday, you may want your code to run on Linux or, god forbid, a Mac! Instead, you could use this and be safe.
C#:
string folder = GetFolderPath() + Path.DirectorySeparatorChar;

That’ll make sure the slash leans in the correct direction based on the platform. Oh, and the next time I see code to parse a file name from a path, I’m going to slap the developer upside the head and mention this method:
C#:
string fileName = Path.GetFileName(fullPath);

Bağlantıları görmek için lütfen Giriş Yap

This class contains a wealth of information about the current environment in which your code is executing. You can get access to the MachineName, the CommandLine, etc…
However, the one property I would like to get developers to use is a simple one:
C#:
//Instead of this
string s = "Blah\r\n";
//do this
string s = "Blah" + Environment.NewLine;

Again, this falls under the case that your code might actually run on a different operating system someday. Might as well acquire good habits now.
 
Your Friendly Neighbor
Süper Üye
Katılım
1 Eyl 2019
Mesajlar
630
Çözümler
2
Tepki puanı
46
Ödüller
5
Yaş
27
6 HİZMET YILI
What did you mean by different os? Is it platform based?
 
Hack Is My Life
Süper Üye
Katılım
11 May 2019
Mesajlar
1,289
Çözümler
3
Tepki puanı
124
Ödüller
6
Yaş
36
7 HİZMET YILI
thanks nice tutorial source
 
Life is free but live need to pay
Süper Üye
Katılım
4 Şub 2018
Mesajlar
703
Tepki puanı
84
Ödüller
7
Yaş
32
8 HİZMET YILI
dont know idea im not coder :D
 
Onaylı Üye
Katılım
21 Şub 2020
Mesajlar
50
Tepki puanı
0
Yaş
33
6 HİZMET YILI
I will give a better study on this code thank you very much for sharing with us
 
Samurai of the seas
Seçkin Üye
Katılım
1 Ağu 2018
Mesajlar
504
Çözümler
3
Tepki puanı
43
Ödüller
7
7 HİZMET YILI
Your genius
 
♥»尊敬«♥
Süper Üye
Katılım
28 Şub 2020
Mesajlar
1,444
Çözümler
4
Tepki puanı
222
Ödüller
5
Yaş
27
6 HİZMET YILI
Thank you so much sir :D
 
Seçkin Üye
Katılım
6 Haz 2019
Mesajlar
491
Tepki puanı
5
Ödüller
3
Yaş
30
7 HİZMET YILI
What did you mean by different os? Is it platform based?
 
DeathZero
Süper Üye
Katılım
15 Mar 2018
Mesajlar
610
Tepki puanı
15
Ödüller
9
Yaş
28
8 HİZMET YILI
Nice Job, Thanks man <3
 
Üye
Katılım
18 Mar 2020
Mesajlar
34
Tepki puanı
1
Ödüller
2
Yaş
35
6 HİZMET YILI
Been using c# for a bit now and you have definitely learned me somthing, thanks!
 
Beerus sama
Ultra Üye
Katılım
7 Şub 2020
Mesajlar
1,552
Çözümler
1
Tepki puanı
207
Ödüller
4
6 HİZMET YILI
i can use this i think i will try
 
Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Üst