开发者

Extending primitive types

开发者 https://www.devze.com 2023-01-08 13:45 出处:网络
Is it possible to extend primitive types such as System.String and System.Int32 (IE: integer) in .Net 4 and if so how?

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 struc开发者_Go百科ture.

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

Thanks all..


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消