开发者

C# Beginner, writing/calling a Method

When we write a method, say an easy one like

  void myMethod()
    {
    // code here
    // 
    }

and we call it from within Main(), it's done essentially this way:

Program myProgram = 开发者_高级运维new Program ();
myProgram.myMethod();

Obviously, myProgram is entirely arbitrary (taking into consideration, of course, coding conventions) but what's up with the reference to Program?


You are declaring your method myMethod inside a class called Program. Since your method is not a static method (i.e. it is not static void myMethod()) it requires an instance of Program in order to work. Therefore you need to create a new instance of Program in order to invoke myProgram.myMethod() on it. If myMethod were static, you could have called it simply by Program.myMethod() or, since you're already inside that class to begin with, myMethod() (since the current class name is implied for static methods).


Program is a class, which contains methods. By default the only method it contains is static void Main(...).

If you add your non-static method myMethod it doesn't belong to the class Program, but rather to instances of Program (called objects).

Static methods (like Main) can be called directly from the class:

Program.Main(...);

Non-static methods must be called from objects of the class:

Program program = new Program();
program.myMethod();

Classes are designed to group together like functionality. Program isn't a good place to put these. You should create other classes, and use them throughout your code.

By using classes you keep like code together, and provide a mechanism to reuse the same code over again from different places. You can create as many different instances of 'Program' as you like, from many different classes, and invoke the 'myMethod' method on each of them.

For instance you might have a ClassRoster and a Student class, which can be used like this in the ClassScheduler class:

ClassRoster roster = new ClassRoster();

Student studentOne = new Student();
studentOne.StudentId = "123456";

roster.EnrollStudent(studentOne);


Program is the type, that defines myMethod in your question. In C# methods are defined by types.

You can either call methods via an instance of the defining type or via the type itself (if the method is static). Since myMethod isn't static, you need an instance of the type Program to call it.


use this:

 public void myMethod()
    {
    // code here

    }

in main method:

Program myProgram = new Program();
myProgram.myMethod();


Thanks everyone. I went back to my code; I added static in front of myMethod, and in doing so the Program myMethod = new Program() became unnecessary (uncompilable? illegal?), and it can be called simply by writing myMethod() Obviously I need to study up on what static does and how it affects methods/classes!

I'm actually only in week #3 of my .NET class... our instructor, while very smart, leaves something to be desired in the teacher category. The assigned text for the class is only so-so in my opinion, at least for me and how I learn (Programming C#, O'Reilly) This is a very good community, thanks!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜