static void Main(string[] args) { // First, we have to create a "class" that has methods we can "call" or "execute". MyMethodsClass methods = new MyMethodsClass(); // Then we use the name we gave your "instance" of the class, and "call" what we need to use. methods.ThisIsAPublicMethod(); NewLine(1); methods.ThisIsAnInternalMethod(); NewLine(1); methods.ThisIsAPublicMethodThatCallsAPrivateMethod(); NewLine(1); methods.ExtendMyClass(); }
/// <summary> /// We can even "extend" our own classes with this. This allows us to "add" code that /// is needed, without changing the main class. There are better ways of doing /// this, however, if the Class we want to change was not created by us, this /// may be the only way to give a class the functionality we want. The "PlusFive" /// is an example of this. We did not create the 'int', but we can "give" it more /// methods that we can use. /// /// Thus, if we do something VERY OFTEN with a class that we did not make, we can /// use this way to extend that. It should ONLY be used when what we are needing /// is used extensively. We can always use other "patterns" to make our own class /// that will do the same thing and it might be more effective. /// /// Notice that this is a public static class and the name is ExtensionMethods. We /// can actually call it anything we want, but most people will call this class /// extension methods, or {type}ExtensionMethods. /// /// For example:: public static class MyMethodsClassExtensionMethods /// /// However, in many cases, regarless of what is being extended, we call it ExtensionMethods /// </summary> /// <param name="input"></param> public static void ExtendMyClass(this MyMethodsClass input) { Console.WriteLine("This is a message from the extension class on our object."); input.ThisIsAPublicMethodCalledFromTheExtensionClass(); }