1

There is an error in List<Story>:

the type or namespace name 'List<>' could not be found (are you missing a using directive or an assembly reference?) (CS0246) [project]

using System;
using project.Models;

namespace project.Models
{
    public class Genre
    {
        public int GenreId { get; set; }

        public string Name { get; set; }
        public List<Story> Stories { get; set; }
    }
}

2 Answers 2

2

You are missing a using statement using System.Collections.Generic;

Sign up to request clarification or add additional context in comments.

1 Comment

using System.Collections.Generic this is all you need
1

Add a reference to System.Collections.Generic to the top of your class:

using System.Collections.Generic;

From your comment:

have another question: is it necessary to write namespace project.Models? and the reason

Namespaces are not mandatory but they help organise your code.

You don't need this line using project.Models; at the top of your class Genre. Genre is already in the namespace project.Models since you define that here:

// define the namespace for Genre
namespace project.Models
{

Since Genre is in the namespace project.Models you can reference any other class in Models without adding an explicit using.

However, when you reference Genre from a class which is in a different namespace (not in project.Models) you will need to add using project.Models to the top of that class. Otherwise, you could use the full reference Models.Genre.

This link has a good explanation of declaring, accessing and using namespaces.

3 Comments

have another question: is it necessary to write namespace project.Models? and the reason
Namespaces are not mandatory but they help organise your code (especially for larger projects). When you use your class Genre in another class you will need to add using project.Models at the top of the class, or use Models.Genre when referencing Genre @DanabekDuisekov
@DanabekDuisekov I've updated my answer with info on namespaces

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.