c# beginner question
What do you call those statements highlighted in red?
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
int choice;
Console.WriteLine("Please select below");
Console.WriteLine();
Console.WriteLine("1. Area of a Circle");
Console.WriteLine("2. Area of a Rectangle");
Console.WriteLine("3. Area of a Cylinder");
Console.WriteLine();
choice = Convert.ToInt32(Console.ReadLine());
if (choice == 1)
{
Console.Clear();
aCircle();
}
if (choice == 2)
{
Console.Clear();
aRectangle();
}
if (choice == 3)
{
Console.Clear();
aCylinder();
}
Console.Write("Do you wish to try again? : ");
Console.ReadLine();
}
static void aCircle()
{
int r;
double A;
Console.WriteLine("Enter the radius:");
r = Convert.ToInt32(Console.ReadLine());
A = 3.14 * r * r;
Console.WriteLine("The Area of circle of given radius is="+A);
Console.ReadLine();
}
static void aRectangle()
{
int w, h;
int ar;
Console.WriteLine("Enter the width:");
w = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the height:");
h = Convert.ToInt32(Console.ReadLine());
ar = w * h;
Console.WriteLine("The Area of rectangle is=" + ar);
}
static void aCylinder()
{
double rc;
double hc;
double ac;
double pi=3.14;
Console.WriteLine("Enter the radius");
rc = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the height:");
hc = Convert.ToInt32(Console.ReadLine());
ac = 2 * pi * rc*rc + 2 * pi * rc * hc;
Console.WriteLine("The Area of cylinder is=" + ac);
Console.ReadLine();
}
}
}
Another question.
This is an example of a constructor method:
public double multiply(double nFirst, double nSecond)
{
}
now what makes it a constructor method and how is it different from a normal method?
Lightning03
Junior Poster in Training
92 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
First Question: Those are static methods. Also known as class methods.
Second Question: That's not a constructor. Constructors don't have a explicit return type, while this one does (double).
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
First Question: Those are static methods. Also known as class methods.
Second Question: That's not a constructor. Constructors don't have a explicit return type, while this one does (double).
oh yeah thanks, if that wasn't a constructor then what do you call it?
Also this is what I meant by constructor:
public mySampleClass()
Lightning03
Junior Poster in Training
92 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
oh yeah thanks, if that wasn't a constructor then what do you call it?
It's a method.Also this is what I meant by constructor:
public mySampleClass()
A constructor has the same name as the class it is in and has no explicit return type. For example
MyClass {
public int Value { get; set; } // Automatic property
public MyClass() { // Parameterless constructor
public MyClass(int v) { // Constructor that takes an int
Value = v;
}
public MyClass(String s) { // Constructor that takes a string
int v;
if (Int32.TryParse(s, out v) == false) {
throw new ArgumentException("Parameter must be an integer");
}
Value = v;
}
}
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558