1

In object-oriented programming, everything is supposed to be an object. Starting from this postula, is it possible to add methods and fields to a literal object, such as a number, a string, a Boolean value or a character?

I noticed that in C#, we can use some methods and fields of the "Integer" class from a mathematical expression:

var a = (2 + 2).ToString();

I imagine that it is more syntactic sugar to access the "Integer" class and a method actually related to the mathematical expression (and / or its value).

But is it possible in C# to define one of the methods and fields to a literal object alone? Such as these examples:

"Hello, world!".print();
var foo = 9.increment();

This would probably be useless, but the language being object-oriented, this should be feasible. If this is not possible in C#, how could we do this with the object-oriented paradigm?

Foxy
  • 980
  • 8
  • 17
  • FYI: Not everything in C# (.NET) is an object. .NET has reference types (from which object instances can be created), and value types (from which you can't create object instances). However, value types can be boxed (wrapped) in objects, which for example allows value types to be passed to methods as arguments of type `Object` (this is being handled automatically) –  Sep 19 '18 at 20:08
  • Not sure what you're asking here - `2` is an int, so `2 + 2` is also an int. `"Hello, world!"` is a string just like the `s` variable in the following code: `string s = "Hello, world!"` – Zohar Peled Sep 19 '18 at 20:08
  • 1
    @elgonzo ["All value types are derived implicitly from the System.ValueType."](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value-types) which inherits from ---> System.Object.... However, the references them selves are neither reference types nor value types - and the same goes for the language's structures such as loops, conditions etc`. So you are correct even if it was for the wrong reasons :-) – Zohar Peled Sep 19 '18 at 20:12
  • 1
    @elgonzo You most certainly can have instances of a value type. In fact, every variable of a value type has an instance in it. It can't not. The only types in C# that aren't objects are pointers, and I think maybe one other fringe type. But basically all types are objects. – Servy Sep 19 '18 at 20:14
  • 1
    Okay, my first comment is indeed incorrect. I'll keep it around so that the comments of ZoharPeled and Servy are not out of context. My apologies, my bad... –  Sep 19 '18 at 20:18
  • Isn't it Ironic that the wrong answer was the only one not downvoted? – Zohar Peled Sep 19 '18 at 20:31

4 Answers4

4

Sure, you can implement an extension method and have the desired syntax (however, Int32 class will not be changed):

  public static class IntExtensions {
    public static int increment(this int value) {
      return value + 1; 
    }
  }

 ...

 // Syntax sugar: the actual call is "int foo = IntExtensions.increment(9);"
 var foo = 9.increment();

In the current C# version (7.2) we can't add extension properties, extension events etc. These options can appear in C# 8.0 (Extension everything, https://msdn.microsoft.com/en-us/magazine/mt829270.aspx):

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

You don't add methods to a given instance of an object, you add methods to a type. Additionally, the language doesn't allow you to define what methods a string (or other type of) literal has, it defines what methods all strings have, of which string literals act just like any non-literal strings, and have exactly the same methods.

Note that (2 + 2) is not an instance of the "Integer" class, it will resolve to an instance of the System.Int32 struct. The difference isn't relevant to this behavior, but it's relevant to lots of others.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • And to be even more accurate, it's not the c# language that define the members of string, int, or any other type built in the framework - it's the framework class library. – Zohar Peled Sep 19 '18 at 20:26
0

You can use extension methods to achieve this, which must be static methods defined in a static class. In you example above, you could do the following:

public static class Extensions
{
    public static int increment(this int num)
    {
        return ++num;
    }
}
Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
akerra
  • 1,017
  • 8
  • 18
  • 1
    This would return the original num and only afterwards increase it by 1. In your case it should be ++num. https://stackoverflow.com/questions/3346450/what-is-the-difference-between-i-and-i – Andreas Sep 19 '18 at 21:09
0
"Hello, world!".print();

This string is an instance of the String Class in C# which inherits from the Object class. So you have to create the print() method in the String Class in order to make this work.

  • 2
    Sorry, but we *can't*: both `String` and `Int32` are *sealed*, that's why they can't be *inherired* like `public class MyString: String {public void print() {...}}`. Both `String` and `Int32` are classes of the standard library which we should not change. – Dmitry Bychenko Sep 19 '18 at 20:23
  • Yeah, should have mentioned explicitly that that was not possible. – Brandon Paris Sep 21 '18 at 13:07