I have just started learning C# a few weeks ago and do have a year and half of experience in Python, so it would be better if i could get a more detailed explanation on why am i getting this error. I have tried searching on google to see if anyone has gotten this error before, but i am not able to understand the solutions and how to put in the solution. Here's the code, find anything wrong?
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();
}
}
}
CodePudding user response:
In the error it says line: 9 File: Program.cs
Your code is in EmptyCSharpFile.cs
There is another .cs file called Program.cs that is in your project that has a Main
method, and that is what is causing the problem. Double clicking on an error should take you to the line of code that is failing.
CodePudding user response:
The posted code is correct.
If you read about the Main()
method, you see that it has the following requirements to be an entry point
Must be
static
method inside a classMust be called
Main()
Can have
public
,internal
,protected
orprivate
access modifiersCan have arguments capturing the command line arguments
static void Main(string[] args) { }
Can have zero arguments
static void Main() { }
Must have only one such method defined within each project.
In your case there might be another file, called
Program.cs
that also defines aMain()
method. Delete this other file, or move the main method fromclass ExecuteRectangle
toclass Program
.For winforms applications the main methods loads the main form and displays it using
Application.Run()
.
On a related note, since you are learning about C# and OOP, consider the code below with similar functionality as yours, but with standard C# syntax, that uses properties, constructors and the ToString()
method to generate string representations of objects
class Rectangle
{
double length;
double width;
public Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
public double Length
{
get { return length; }
set
{
length = value;
}
}
public double Width
{
get { return width; }
set
{
width = value;
}
}
public double Area
{
get { return length * width; }
}
public override string ToString()
{
return $@"Length {Length}
Width {Width}
Area {Area}";
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle(4.5, 3.5);
Console.WriteLine(r);
//Length: 4.5
//Width: 3.5
//Area: 15.75
r.Length = 6.5;
Console.WriteLine(r);
//Length: 6.5
//Width: 3.5
//Area: 22.75
Console.ReadLine();
}
}
I specifically made the properties mutable (can be changes) because this is a common design choice. If the properties were to be immutable then there wouldn't be a set
method, and the fields would have a readonly
keyword in front of them.