13 Replies - 1230 Views - Last Post: 27 January 2014 - 07:45 AM Rate Topic: -----

#1 programmingfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-January 14

why is the static keyword required in this example code?

Posted 26 January 2014 - 03:54 PM

class Program
        {
            //Accept two input parameter and returns two out value
            public static void rect(int len, int width, out int area, out int perimeter)
            {
                area = len * width;
                perimeter = 2 * (len + width);
            }
            static void Main(string[] args)
            {
                int home, work;
                // passing two parameter and getting two returning          value
                Program.rect(5, 4, out home, out work);
                Console.WriteLine("Area of Rectangle is {0}\t", home);
                Console.WriteLine("Perimeter of Rectangle is                {0}\t", work);
                Console.ReadLine();
            }
        }


why is the static keyword required in the method signature for the rect() method. It will not compile if it is absent. why?

the same is true for this example:

    class Program
    {

        static void printvalues(params int[] passedin) {


            foreach (var printthis in passedin)
            {
                Console.WriteLine(printthis);
            }
        
        }

        static void Main(string[] args)
        {

            int[] arrayvals = { 1, 1, 2 };
           printvalues(arrayvals);
        }
    }


This code won't compile without the static keyword in the printvalues() method signature. why?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: why is the static keyword required in this example code?

#2 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2668
  • View blog
  • Posts: 4,786
  • Joined: 24-September 10

Re: why is the static keyword required in this example code?

Posted 26 January 2014 - 03:57 PM

It won't compile because you're accessing the method rect on line 13 as if it is a static method. So thusly you must declare it as a static method.

Note the class 'Program' and it's static method 'Main' act as the entry point to the program. No instance is created, so no instance level method can be accessed. If you want to access an instance method, then create a class with said instance method, instantiate it, and call the method.
Was This Post Helpful? 0
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: why is the static keyword required in this example code?

Posted 26 January 2014 - 03:58 PM

Because Main is static. You can't call non-static methods from a static methods without first creating an instance of the class that the methods resides in.

In your case to make it work without the static keyword, you need to make an object:

Program p = new Program();
p.printValues(1,2,3);


(Although in reality all the other methods should be in a separate class - not in Program alongside Main.
Was This Post Helpful? 0
  • +
  • -

#4 programmingfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-January 14

Re: why is the static keyword required in this example code?

Posted 26 January 2014 - 04:08 PM

Ok, I understand now.


So, essentially the entire program is itself a class. So one way is to create an instance of the class "program" and access the function like ryano121 said. Another way is to make the function static such that it can be accessed globally without the need to create an instance of the class "program". Got it.

thanks.
Was This Post Helpful? 0
  • +
  • -

#5 Michael26  Icon User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: why is the static keyword required in this example code?

Posted 26 January 2014 - 04:18 PM

You should read more about Object oriented programming.
You could start here by reading about classes and objects
Was This Post Helpful? 0
  • +
  • -

#6 programmingfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-January 14

Re: why is the static keyword required in this example code?

Posted 26 January 2014 - 04:25 PM

View Postlordofduct, on 26 January 2014 - 03:57 PM, said:

Note the class 'Program' and it's static method 'Main' act as the entry point to the program. No instance is created, so no instance level method can be accessed. If you want to access an instance method, then create a class with said instance method, instantiate it, and call the method.


Ok, I have another related question. Let's say I do something thing like this:

class Program
        {
            //Accept two input parameter and returns two out value
            public static void printit(string mycountry)
            {
               Console.WriteLine(mycountry);
            }
            static void Main(string[] args)
            {
                   Program p = new Program();
                   p.printit("United States");

            }
        }



You are creating an instance of a class within its own class definition/structure. How is that permitted?
Was This Post Helpful? 0
  • +
  • -

#7 Michael26  Icon User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: why is the static keyword required in this example code?

Posted 26 January 2014 - 04:29 PM

using System;

class Program
{
    static void Main()
    {
	// Cannot declare a variable of type Perl.
	// This won't blend.
	// Perl perl = new Perl();

	// Program is a regular class so you can create it.
	Program program = new Program();

	// You can call static methods inside a static class.
	Perl._ok = true;
	Perl.Blend();
    }
}

static class Perl
{
    // Cannot declare instance members in a static class!
    // int _test;

    // This is ok.
    public static bool _ok;

    // Can only have static methods in static classes.
    public static void Blend()
    {
	Console.WriteLine("Blended");
    }
}

Was This Post Helpful? 0
  • +
  • -

#8 programmingfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-January 14

Re: why is the static keyword required in this example code?

Posted 26 January 2014 - 04:31 PM

View PostMichael26, on 26 January 2014 - 04:18 PM, said:

You should read more about Object oriented programming.
You could start here by reading about classes and objects


I know about objects and classes already but I overlooked the fact that the entire code itself was a class. If I had realized that I would not have been confused.

I have a related question. Look at this code:

class Program
        {
            //Accept two input parameter and returns two out value
            public void printit(string mycountry)
            {
               Console.WriteLine(mycountry);
            }
            static void Main(string[] args)
            {
                   Program p = new Program();
                   p.printit("United States");

            }
        }



You are creating an instance of the "Program" class within its own class structure. How is this possible?
Was This Post Helpful? 0
  • +
  • -

#9 Michael26  Icon User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: why is the static keyword required in this example code?

Posted 26 January 2014 - 04:33 PM

I already posted response, it is #7, your program class is not static. Try to put
Program p = new Program();

outside of scope of the program class and see what happens.

This post has been edited by Michael26: 26 January 2014 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#10 programmingfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-January 14

Re: why is the static keyword required in this example code?

Posted 26 January 2014 - 04:45 PM

View PostMichael26, on 26 January 2014 - 04:29 PM, said:

using System;

class Program
{
    static void Main()
    {
	// Cannot declare a variable of type Perl.
	// This won't blend.
	// Perl perl = new Perl();

	// Program is a regular class so you can create it.
	Program program = new Program();

	// You can call static methods inside a static class.
	Perl._ok = true;
	Perl.Blend();
    }
}

static class Perl
{
    // Cannot declare instance members in a static class!
    // int _test;

    // This is ok.
    public static bool _ok;

    // Can only have static methods in static classes.
    public static void Blend()
    {
	Console.WriteLine("Blended");
    }
}


I understand your point. In other words, you cant instantiate the Perl class within the main method because the class definition for Perl is not contained with in the Program class. You either create it within the "Program" class or create a class library project within the solution that you are working on. I understand, but why did you have to declare the Perl class with a static keyword? I removed it and your code above is still compiling and behaving fine.

thanks.
Was This Post Helpful? 0
  • +
  • -

#11 Michael26  Icon User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: why is the static keyword required in this example code?

Posted 26 January 2014 - 04:55 PM

You can't create instance of the static class, MSDN has explanation. You can't create instance of Perl because Perl is static and Program class in not static. If you remove the static and remove comment from perl instance it will work because perl class is no longer static.
Was This Post Helpful? 0
  • +
  • -

#12 programmingfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-January 14

Re: why is the static keyword required in this example code?

Posted 26 January 2014 - 05:03 PM

View PostMichael26, on 26 January 2014 - 04:55 PM, said:

You can't create instance of the static class, MSDN has explanation. You can't create instance of Perl because Perl is static and Program class in not static. If you remove the static and remove comment from perl instance it will work because perl class is no longer static.



I don't understand why you would want to declare the Perl class as static. You could do most of everything without it. declaring a class as static is appropriate in nested classes but I dont understand why you would need it in your code example above. You can still access the blend() method with "Perl.Blend()" without having to create an instance of Perl. So what is the use of declaring the Perl class as static?
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6114
  • View blog
  • Posts: 21,049
  • Joined: 05-May 12

Re: why is the static keyword required in this example code?

Posted 26 January 2014 - 10:19 PM

There are times when you don't want anybody using your code to be able to instantiate your class. This is when you would use the static modifier for the class. For example, if you are just using the class to be a container for a set of related methods. See the Enumerable class as an example.

In this case, Michael26, used the modifier on the class to help illustrate that the class could only be used as as a static class.
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: why is the static keyword required in this example code?

Posted 27 January 2014 - 07:45 AM

I'll also encourage you to read more on OOP and C# objects, because you're making some incorrect assumptions. One such:

"You are creating an instance of the "Program" class within its own class structure. How is this possible?"

It's possible because that's how the language designers designed the language. You can create instances of classes inside their definition, it's not an issue. In fact, we do that frequently.

And this one:

"You can still access the blend() method with "Perl.Blend()" without having to create an instance of Perl. So what is the use of declaring the Perl class as static? "

You can only access the Blend method with "Perl.Blend()" because the Blend method is static. If it weren't, you would get a compiler error.

The confusion is coming from you making your class static. Static classes can only contain static members. It's an optimization and correctness thing. But what matters is the static modifier on the method itself. Making classes static is for things like Constants classes; something that holds values or methods that aren't instance specific, and you don't want it to even be capable of holding anything that is.

Read up more on OOP to get better at this stuff.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1