Why do some Methods not require a specified class? (Extremely basic question)

Cshape65d

New member
Joined
Jan 11, 2022
Messages
4
Programming Experience
Beginner
I apologize for such a basic question, but I'm extremely new to C# and can't seem to confidently answer this question:

Why does something like:
string myStringVariable2 = myStringVariable.ToUpper()
not require me to call the String Class?

But something like:
int myIntVariable2 = Math.Abs(myIntVariable)
does require the Math class call-out.

Is ToUpper an "extension" variable and this is why it's not necessary?
Are all methods that can be called with just a period and the method name attached to your variable considered "extension" variables?
When I look up the ToUpper method in the String class, it looks like it's built like other methods so I don't understand why it's not written the same way when referenced. I was expecting to see the use of "this" as a parameter but don't see it.

Please note: any guidance the terms/phrases/terminology used in this question would be greatly appreciated. Part of not being able to find an answer on my owns comes to my very limited and likely inaccurate C# vocabulary.

Thank you very much
 
Last edited:
Class methods / instance methods are by themselves not initialized... because they are an inherent part of the class definition they require the class that contains them to be initialized before they can be used. Normally it's easy to spot an initialization of a class by the use of the new keyword; however that's hardly ever used for a class like string; similarly for classes that are initialized using helper static methods. Irrespective of how they are initialized, the instance methods / class methods cannot be accessed until the class is initialized.


Static methods are not instance methods.
It's as I mentioned before; useful to consider that static methods are simply created within a class structure to collectively catalogue it, for example:
  • The mathematic static methods are housed in a class called Math.

Also a class can have both class methods / instance methods and static methods (aka functions) within the same class definition; however the class methods will not be available for use until an instance of the class has been created, whereas the static methods will be available at all times. For example:

C#:
public class Person {
    public int ID;
    public string Lastname;
    // initialiser
    public Person(int id, string lastname) => (ID, Lastname) = (id, lastname);
    // static method (helper method used to initialize an instance of Person)
    public static Person New(int id, string lastname) => new(id, lastname);
    // class nethod
    public override string ToString() => $"ID: {ID}, Lastname: {Lastname}";
}

static public void Main() {
    // Error attempt to access class / instance method before class is initialized
    Console.WriteLine(Person.ToString());

    // No problem to call the static member before the class Person is initialized
    var person1 = Person.New(1, "Jack");  // this returns an intialized instance of Person.

    // Once we have an instance of Person, we then can access the class / instance methods.
    Console.WriteLine(person1.ToString());

    // Initializing and instance of Person using the new keyword
    var person2 = new Person(1, "Jack"); // this has the outcome as calling the static method New.
}

I am very grateful for this clarifications. So I'd edit my comments as:

Class Methods (a.k.a. Instance Methods or Non-static methods)
  • Class initialization is required for non-static methods and is done by creating a new instance of the class. A new instance of the Class is often created via:
    • 1. Using the "new" keyword,
    • 2. Creating a new variable (e.g. a string variable) often without the "new" keyword, or
    • 3. Calling a static helper method within the class
    • Non-static methods (and extension methods) can be called via dot method extensions.
Static Methods
  • Class initialization is also required for static methods, but:
    • Class initialization for Static Methods does not require a new instance if both the class and method are static (e.g. int myIntVariable2 = Math.Abs(myIntVariable) )
    • Class initialization for Static Methods does require a new instance if the class itself is not static (regardless of whether the method is static)
      • Creating this new instance can be done in the same ways mentioned above for class methods class initialization
      • e.g. Calling endofunk's Static Person helper method within his non-static Person Class

Please tell me that's correct :oops: ,lol. And again, a million thanks for everyone's insights. I'm learning way more than even the answer to my initial question in the process.
 
Last edited:
...
Please tell me that's correct :oops: ,lol. And again, a million thanks for everyone's insights. I'm learning way more than even the answer to my initial question in the process.
I suggest you learn to test your conclusions, for example, you state that "class initialization is also required for static methods... "
C#:
public class Test1 {
    public static void DoesITWork() {
        Console.WriteLine("Test1 worked");
    }
}

public static class Test2 {
    public static void DoesITWork() {
        Console.WriteLine("Test2 worked");
    }
}

static public void Main() {
    // no initialization of either clas
 
    Test1.DoesITWork(); // Test1 worked
    Test2.DoesITWork(); // Test2 worked
}
Both worked so static methods do not required the class they reside in to be initialized prior to their use.
Ps. if they did then you couldn't have helper static methods

Also string is an interesting example to explore the flexibility extent of class construction. You can find Microsoft's implementation of that here: https://github.com/microsoft/referencesource/blob/master/mscorlib/system/string.cs
 
Last edited:
I suggest you learn to test your conclusions, for example, you state that "class initialization is also required for static methods... "
C#:
public class Test1 {
    public static void DoesITWork() {
        Console.WriteLine("Test1 worked");
    }
}

public static class Test2 {
    public static void DoesITWork() {
        Console.WriteLine("Test2 worked");
    }
}

static public void Main() {
    // no initialization of either clas
 
    Test1.DoesITWork(); // Test1 worked
    Test2.DoesITWork(); // Test2 worked
}
Both worked so static methods do not required the class they reside in to be initialized prior to their use.
Ps. if they did then you couldn't have helper static methods

Also string is an interesting example to explore the flexibility extent of class construction. You can find Microsoft's implementation of that here: https://github.com/microsoft/referencesource/blob/master/mscorlib/system/string.cs

100% got it. I misread an earlier comment that sent me down the wrong path. Additionally, I now have Visual Studio installed and am testing/experimenting with all the code posted in the replies above. Lastly, I'll take a close look at Microsoft's implementation (and other content on that site). I should be good to go. Thanks again (y)

Side note: I finally finished this Youtube C# intro tutorial (among many others) and the "Static methods and Classes" chapter at the 4 hour and 14 minute mark also covers these concepts.
 
Last edited:

Latest posts

Back
Top Bottom