2

Is it possible to extend primitive types such as System.String and System.Int32 (IE: integer) in .Net 4 and if so how?

To be more specific, I am aware of the concept of partial classes but this doesnt seem to be the answer. Also I find that System.String is not inheritable and Int32 is a structure.

Lastly I am interested in knowing both a VB.Net and C# answer to the above question.

Thanks all..

Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243

2 Answers2

14

You cannot extend them directly - the String class is sealed, for example, and as you noted value type wrappers (such as Int32) are normally structs.

You can write extension methods (C#, VB.NET) to them, that's what they are there for.

Another option, is to write a wrapper around these, exposing all of their properties and adding more functionality.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • I didn't downvote, please explain how we can wrap String. – Anirudha Gupta Jun 19 '18 at 11:08
  • 1
    @Adrian - create a new class, put in a private `string` field (pass a `string` in to the contractor, assigning the passed in parameter to the field. Now you can write any wrapper functions you want - passing any to the string field as needed. You will not get compiler/language support for it as you do for strings, but that's how it goes. – Oded Jun 19 '18 at 12:57
1

Just as additional info (Oded is right already on the other stuff):

There are no "primitive types" in .Net. Only classes and value types (called structures in C#) (and all are decendents of object).

However you cannot inherit from value types (like int, byte, ...) and you cannot inherit from sealed classes (like string).

Foxfire
  • 5,675
  • 21
  • 29
  • 1
    sry but that's inaccurate not all types derives from object http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx – Rune FS Jul 25 '10 at 10:24
  • 2
    I never wrote that ALL types derive from object. But classes and value types DO! Citation from YOUR linked page: "A great many types do derive from object. All value types, including enums and nullable types, derive from object." – Foxfire Jul 26 '10 at 15:26
  • 1
    You *did* write that all types derive from object by saying "Only classes and value types (called structures in C#) (and all are decendents of object)", and this is incorrect. There are primitive types in .Net. They map to structures, and they don't derive from object. http://msdn.microsoft.com/en-us/library/aa711900(v=vs.71).aspx – Hank Schultz Jan 07 '14 at 18:47
  • 1
    I don't think Foxfire was trying to say literally *all* types derive from `object`, but what he is saying is that classes and value types are descendants of `object`. - See this from Eric Lippert himself: http://stackoverflow.com/a/1682604/7270462 > All C# types, including primitive types such as int and double, inherit from a single root object type. - Also see this regarding value types being aliases: https://msdn.microsoft.com/en-us/library/ya5y69ds.aspx - Use the "Object Browser" in Visual Studio and find that the `System.ValueType` does indeed have `Object` as its base type. – ErrCode Apr 18 '17 at 08:38