How to resolve Class, struct, or interface method must have a return type?


C# error, when trying to execute the code as below

using System;
namespace Examples {
 class Program {
  public static void Main(string[] args) {
   Laptop Lenovo = new Laptop("Lenovo");
   Price("$");
   Processor("i");
   Ram(2);
   HDD(500);
   Laptop Dell = new Laptop("Dell");
   Price("$");
   Processor("i");
   Ram(4);
   HDD(1);
   Laptop Sony = new Laptop("Sony");
   Price("$");
   Processor("i");
   Ram(8);
   HDD(1);
  }
 }
 class Laptop: LaptopBase {
  public LaptopBase(String LaptopName): base(LaptopName) {
   this.LaptopBase = LaptopName;
  }
  class LaptopBase {
   public LaptopBase(String LaptopName) {
    WriteLine(" " + LaptopName);
   }
   public void Price() {
    WriteLine("$" + 1000, "$" + 2000, "$" + 3000);
   }
   public void Processor() {
    WriteLine("i" + 3, "i" + 5, "i" + 7);
   }
   public void Ram() {
    WriteLine(2 + "GB", 4 + "GB", 8 + "GB");
   }
   public void HDD() {
    WriteLine(500 + "GB", 1 + "TB", 1 + "TB");
   }
  }
 }
}

main.cs(31,10): error CS1520: Class, struct, or interface method must have a return type


Asked by:- Pa
0
: 5151 At:- 11/10/2017 10:24:04 AM
C# Class struct or interface method must have a return type







1 Answers
profileImage Answered by:- bhanu

Not sure what you were trying to achieve, but looking at your code, I have corrected these points to make it execute properly and have commented out the code so that you can understand

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        public static void Main(string[] args)
        {
            //Create insatce of Class with Name as "Lenove"
            Laptop Lenovo = new Laptop("Lenovo");
           
            //Using the above instance called the Price Method of Lenovo class, it will print $1000
            Lenovo.Price();
            //Using the above instance called the Price Method of Lenovo class, it will print i3
            Lenovo.Processor();
        


        }
    }
    //Laptop Class inherited from LaptopBase class
    public class Laptop : LaptopBase
    {
        public Laptop(string LaptopName) : base(LaptopName)
        {
            //this is used to set value of constructor
            this.LaptopBase = LaptopName;
        }

        public string LaptopBase { get; }
    }
    //Laptop Base class
    public class LaptopBase
    {
        public LaptopBase(String LaptopName)
        {
            //use Console.WriteLine to print data
            Console.WriteLine(" " + LaptopName);
        }

        public void Price()
        {
            Console.WriteLine("$" + 1000, "$" + 2000, "$" + 3000);
        }
        public void Processor()
        {
            Console.WriteLine("i" + 3, "i" + 5, "i" + 7);
        }
        public void Ram()
        {
            Console.WriteLine(2 + "GB", 4 + "GB", 8 + "GB");
        }
        public void HDD()
        {
            Console.WriteLine(500 + "GB", 1 + "TB", 1 + "TB");
        }
    }
}

try to execute it here online, it will work and provide you some output

0
At:- 11/10/2017 1:21:38 PM
Thanks a ton!!! 0
By : Pa - at :- 11/15/2017 10:07:29 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use