Having Trouble Understanding When To Call Classes In C# With Mono and .NET...

blackneos940

Active Member
So anywho, this post pertains to Linux, but also to any OS that can run Mono or .NET Framework. It's a basic C# Program I got from a YouTube video teaching C#. :) First, the Code...

Code:
using static System.Console;

namespace Classes

{
  class Animal

  {
    //Class Constructors

    public static int count;

    public string name;

    public int age;

    public float happiness;

    public Animal()

    {
      //Class Methods

      name = "Spotty";

      age = 3;

      happiness = 0.5f;

      count++;

    }

    public Animal(string _name, int _age, float _happiness)

    {
      name = _name;

      age = _age;

      happiness = _happiness;

      count++;

    }

    public void Print()

    {
      WriteLine("Name: " + name);

      WriteLine("Age: " + age);

      WriteLine("Happiness: " + happiness);

    }

  }

  class Main_Class

  {
    public static void Main(string[] args) //This is a Method called "Main". It is called when the Program starts.

    {
      Animal dog = new Animal();

      dog.Print();

      Animal cat = new Animal("Mr. Beans", 2, 0.08f);

      cat.Print();

      WriteLine("Num of Animals: " + Animal.count);

      ReadKey();

    }

  }

}
I'm having trouble knowing when to use public and private, and when to call Classes. :( And in particular... Why is public Animal() in there twice? That's seriously confusing..... I feel that if I can understand these things, I can become much better at C#, and OOP in general. :) It Compiles and runs under Mono and .NET (it doesn't use Windows Forms, and is a CLI Program). :3 Thanks for any help guys. :3 Have a good one..... :3
 


JasKinasis

Well-Known Member
Public is for class members/methods that can be directly accessed by any external users of the class.

Private is for data members/methods that are used internally by the class. They can only be accessed/used internally, by the class.

And then you have protected, which is a variant of private, which is for members/methods that can be used by derived classes. So any of the parent classes attributes that are marked as protected could be accessed by child classes that are derived from it.

The two public Animal methods are constructors. The one without parameters is the default constructor. Which will create and initialise an instance of the class and set some default values/attributes.

The version that takes parameters is an explicit constructor. The parameters to the constructor allow you to create a fully initialised instance of the class.

So instead of calling the default constructor and then individually changing the values of members that need to be changed, you can call the explicit constructor and set all of the values upfront.

If that makes sense?
 

blackneos940

Active Member
Public is for class members/methods that can be directly accessed by any external users of the class.

Private is for data members/methods that are used internally by the class. They can only be accessed/used internally, by the class.

And then you have protected, which is a variant of private, which is for members/methods that can be used by derived classes. So any of the parent classes attributes that are marked as protected could be accessed by child classes that are derived from it.

The two public Animal methods are constructors. The one without parameters is the default constructor. Which will create and initialise an instance of the class and set some default values/attributes.

The version that takes parameters is an explicit constructor. The parameters to the constructor allow you to create a fully initialised instance of the class.

So instead of calling the default constructor and then individually changing the values of members that need to be changed, you can call the explicit constructor and set all of the values upfront.

If that makes sense?
Yeah, it makes sense! :3 Thank you! :3 Jas comin' to save the day again!! :3
 

Members online


Top