Can someone please explain in detail what the lines in this code does?

rei

New member
Joined
Sep 2, 2015
Messages
4
Programming Experience
Beginner
I am new to C# and this code was on one of the tutorials but it didn't give much of an explanation of what each line does.

The code:
using System;
namespace RectangleApplication
{
     class Rectangle
     {
           double length;
           double width;
           public void Acceptdetails()
           {
                 length = 4.5;
                 width = 3.5;
            }
            public double GetArea()
            {
                  return length * width;
            }
            public void Display()
            {
                  Console.WriteLine("Length:  {0}", length);
                  Console.WriteLine("Width:  {0}", width);
                  Console.WriteLine("Area:  {0}", GetArea());
             }
      }
     class ExecuteRectangle
     {
           static void Main(string[] args)
           {
                Rectangle r = new Rectangle();
                r.Acceptdetails
                r.Display();
                Console.ReadLine();
          }
     }
}
 

Attachments

  • try-it.jpg
    try-it.jpg
    1.5 KB · Views: 62
Last edited by a moderator:
What do you think a method named "GetArea" does? What do you think a method named "Display" does? The Main method creates a Rectangle, accepts the details for that Rectangle and then displays it. Exactly what part is confusing you?
 
I don't understand line 6-7, why is "double" there and what does it mean? And are "Acceptdetails" "Display" and "GetArea" custom methods? Sorry if I'm making no sense, I am completely new to this.
 
We are obviously here to help with C# programming questions but we're not here to teach you how to program in C# from scratch. The questions you are asking are really about the absolute basics and you can find information about the basics of programming already out there in loads of places. I would suggest that you start by following the Tutorial link in my signature below and work your way through that from start to finish. If and when you have genuine issues, by all means come back and we'll see whgat we can do but forums like this are not about providing basic information that already exists in many places and can be found easily. They are for helping with complex problems that aren't already dealt with directly elsewhere.
 
I don't understand line 6-7, why is "double" there and what does it mean?
In C# you need to tell the compiler the type of a variable. You (as a person) know that 10.5 is a number and that "hi" is a word. For the available built-in types, see https://msdn.microsoft.com/en-us/library/ya5y69ds.aspx

And are "Acceptdetails" "Display" and "GetArea" custom methods?
I don't know what custom methods are, so can't say for sure. I might use custom methods without knowing :( (what's in a name :))
 
Thanks for the explanation but I already figured it out myself by the time I read your reply. But thanks anyway. And thank you jmcilhinney, the beginner link you gave me helped me understand the code I posted completely! :lemo:
 
Back
Top Bottom