3

I'd like to add a method to the Int32 to do a "FromSomething" stuff.

Something that could allow me to write something like this:

Int32 i = Int32.FromSomething(mySourceData);

How could I achieve this?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Sierramike
  • 371
  • 4
  • 16
  • What is `mySourceData` exactly? What do you want to get from it with `FromSomething` method? – Soner Gönül Feb 26 '15 at 09:24
  • @user3021830: No, extension methods don't allow you to add *static* methods to a type. – Jon Skeet Feb 26 '15 at 09:24
  • (The duplicate is talking about static classes specifically, but it's the same idea - trying to add a static method to an existing class. You can't do that.) – Jon Skeet Feb 26 '15 at 09:25
  • @user3021830: I don't know what you mean by that, but `System.Int32` is an existing *struct* (not class); `int` is the alias for it in C#. – Jon Skeet Feb 26 '15 at 09:27
  • @JonSkeet, nothing but I just need to sleep more :) – user3021830 Feb 26 '15 at 09:28
  • @Soner Gönül: In fact, I'd like to add a ".FromDecimalOrHex(string data)" and mySourceData could be either "10" (decimal value) either "0x10" (hexadecimal value) and put the logic of detecting the type inside this extension method. I ended up creating a Int32Helper class containing my static method. Thanks for your help. – Sierramike Feb 26 '15 at 13:46

0 Answers0