Home > Net >  Need help in understanding C# Classes and Objects
Need help in understanding C# Classes and Objects

Time:03-25

I am learning Visual Studio C#. I am having some difficulty. My background is procedural programs like Basic & Cobol. Below is some sample code for a console program in C#.

I get most of it, but I don't get this part.

           Box box = new Box();
           Box box2; 
           box2 = new Box();

The above statements are where I get confused.

Box box = new Box();

This is how you, well I don't know the technical names but it allow you to use the class along with the code(methods) and variables (properties).

These two lines do the same thing.

           Box box2; 
           box2 = new Box();

What does each line do? I know that a copy of some sort is created from the Box Class. I know that you reference the class with the new name of box or whatever you have choosen. What does each line do? Is the first line just creating a link/reference to the original class? When is memory actually reserved? When do the variables/properties of the class get assigned values/initilized?

Like I said I have written procedural code for a number of years and each command has a function. I am used to knowing the exact function of each command. How it affects the computer and memory. Can anyone help explain what is going on in the computer? Memory?

Thanks.

The entire code is below.

    using System;
    
    namespace ClassesAndObjects
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Create Object Class Type Box
                Box box = new Box();
                Box box2; 
                box2 = new Box();
    
                Console.WriteLine("Enter Length: ");
                double n1 = double.Parse(Console.ReadLine());
                Console.WriteLine("Enter Breath: ");
                double n2 = double.Parse(Console.ReadLine());
                Console.WriteLine("Enter Height: ");
                double n3 = double.Parse(Console.ReadLine());
    
    
                //setting values
                box.Length = n1;
                box.Breath = n2;
                box.Height = n3;
                double volumn = box.getVolumn();
                double area = box.getArea();
    
                Console.WriteLine($"Box Dimensions Are: {box.Length}, {box.Breath}, {box.Height}");
                Console.WriteLine($"Box Volumn is: {volumn}");
                Console.WriteLine($"Box Area is: {area}");
    
    
                // box 2
                // you don't need to define n1, n2 & n3. 
                // They were already defined in box 1 
                // so the word double is not needed in front of
                // n1, n2 & n3
                Console.WriteLine("Enter Length box 2: ");
                n1 = double.Parse(Console.ReadLine());
                Console.WriteLine("Enter Breath box 2: ");
                n2 = double.Parse(Console.ReadLine());
                Console.WriteLine("Enter Height box 2: ");
                n3 = double.Parse(Console.ReadLine());
    
    
                //setting values
                box2.Length = n1;
                box2.Breath = n2;
                box2.Height = n3;
                //double volumn = box.getVolumn();
                //double area = box.getArea();
                // instead of storing the answers in variables 
                // we just call the method in the console writeline statement
                Console.WriteLine($"Box Dimensions Are: {box2.Length}, {box2.Breath}, {box2.Height}");
                Console.WriteLine($"Box Volumn is: {box2.getVolumn()}");
                Console.WriteLine($"Box Area is: {box2.getArea()}");
    
            }
        }
    
        class Box
        { 
            public double Length { get; set; }
            public double Breath { get; set; }
            public double Height { get; set; }
    
            // Here is a method
            public double getVolumn()
            {
                return Length * Breath * Height;
            }
            public double getArea()
            {
                return Length * Breath;
            }
        }
    }




CodePudding user response:

Box box2; 

does not create a Box object. It creates a slot that can hold a reference to a Box object. (I would use the word 'box' instead of 'slot' but that would be confusing)

box2 = new Box();

now we create a box object and place a reference to it in the slot called 'box2'

We can do it all in one line too

Box box3 = new Box();

We can change which Box instance box3 points at

box3 = box2

Both box2 and box3 now point at the same object. The Box that box2 originally pointed at will now disappear, nobody is pointing at it anymore, we cannot get it back , it will be Garbage Collected

CodePudding user response:

In the simplest terms...

Box box = new Box();
Box box2; 
box2 = new Box();

Line 2 says you are going to have a variable called "box2" and that it is not yet but one day will be a Box.

Line 3 actually makes it a Box.

Line 1 does them both at the same time.

CodePudding user response:

For reference types like Box only an address is assigned in memory which creates a reference to the actual memory location where the current value is stored.

The line Box box; declares the variable box and an address is assigned in memory.

When followed by box = new Box() you are assigning the instance of the Box class to your declared variable.

Declaring and assigning the value to the variable Box box = new Box();, basically does both consecutively.

Primitive types in C# (like int, float,...) work differently, since they immediately have a default value assigned.

  • Related