Working with C# lists

2019-09-17 02:16发布

问题:

I'm trying to understand how does lists work in C# but I can't run my application because I get those messages:

Error 1 Inconsistent accessibility: property type 'ClaseLista.ListNode' is less accessible than property 'ClaseLista.List.PrimerNodo' C:\Documents and Settings\Usuario\Escritorio\Listas\ClaseLista\List.cs 19 25 ClaseLista


Error 2 Inconsistent accessibility: property type 'ClaseLista.ListNode' is less accessible than property 'ClaseLista.List.UltimoNodo' C:\Documents and Settings\Usuario\Escritorio\Listas\ClaseLista\List.cs 24 25 ClaseLista


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

using ClaseLista;

namespace Listas
{
    class Program
    {
        static void Main(string[] args)
        {
            List Lista1 = new List();
            int opcion = 1;
            while (opcion > 0 && opcion < 3)
            {
                Console.WriteLine("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
                Console.WriteLine("x Menú Principal (dos datos) x");
                Console.WriteLine("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
                Console.WriteLine("x                            x");
                Console.WriteLine("x 1: Insertar Alumnos        x");
                Console.WriteLine("x 2: Imprimir Lista          x");
                Console.WriteLine("x 3: Salir                   x");
                Console.WriteLine("x                            x");
                Console.WriteLine("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
                Console.Write("Ingrese opción: ");
                opcion = int.Parse(Console.ReadLine());
                switch (opcion)
                {
                    case 1: int numero; string nombre, codigo;
                        Console.Write("Ingrese número de elementos: ");
                        numero = int.Parse(Console.ReadLine());
                        for (int i = 1; i <= numero; i++)
                        {
                            Console.WriteLine("Datos del alumno " + i);
                            Console.Write("Ingrese Nombre: ");
                            nombre = (Console.ReadLine());
                            Console.Write("Ingrese Codigo: ");
                            codigo = (Console.ReadLine());
                            Lista1.InsertaInicio(nombre, codigo);
                        }
                        break;
                    case 2:
                        if (Lista1.EsVacio())
                        {
                            Console.WriteLine("Lista Vacia");
                        }
                        else
                        {
                            Lista1.Imprimir();
                        }
                        break;
                }
            }
        }
    }
}

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

namespace ClaseLista
{
    public class List
    {
        //Constructor
        private ListNode primerNodo;
        private ListNode ultimoNodo;
        public List()
        {
            primerNodo = null;
            ultimoNodo = null;
        }
        //Propiedades
        public ListNode PrimerNodo
        {
            get { return primerNodo; }
            set { primerNodo = value; }
        }
        public ListNode UltimoNodo
        {
            get { return ultimoNodo; }
            set { ultimoNodo = value; }
        }
        //insertar al inicio
        public void InsertaInicio(object nom, object cod)
        {
            if (EsVacio())
                primerNodo = ultimoNodo = new ListNode(nom, cod, null);
            else
            {
                primerNodo = new ListNode(nom, cod, primerNodo);
            }
        }
        //comprobar si es vacio
        public bool EsVacio()
        {
            return primerNodo == null;
        }
        //Imprimir
        public void Imprimir()
        {
            ListNode current = primerNodo;
            while (current != null)
            {
                Console.WriteLine("|" + current.Nombre + " " + current.Codigo);
                current = current.Siguiente;
            }
        }
    }
}

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

namespace ClaseLista
{
    class ListNode
    {
        //Constructor
        private object nombre;
        private object codigo;
        private ListNode siguiente;
        public ListNode()
        {
            nombre = null;
            codigo = null;
            siguiente = null;
        }
        public ListNode(object nomb, object cod, ListNode sig)
        {
            nombre = nomb;
            codigo = cod;
            siguiente = sig;
        }
        //Propiedades
        public object Nombre
        {
            get { return nombre; }
            set { nombre = value; }
        }
        public object Codigo
        {
            get { return codigo; }
            set { codigo = value; }
        }
        public ListNode Siguiente
        {
            get { return siguiente; }
            set { siguiente = value; }
        }
    }
}

What can I do?

回答1:

You need to make ListNode public as well...

public class ListNode


回答2:

it's simply an issue to do with the visibility of your ListNode class - which is declared as

class ListNode

(And therefore has internal visibility)

Whereas your List class is declared as

public class List

Because List then declares one or more public properties of type ListNode, ListNode must also be public:

public class ListNode

Will fix it.



回答3:

This is because you have ListNode defined as:

namespace ClaseLista
{
    class ListNode
    {

By default, the compiler gives this an internal accessibility. To get rid of the error, change this to be public:

namespace ClaseLista
{
    public class ListNode
    {

This is required because you define part of your public API using the ListNode class:

// This can't be public unless "ListNode" is public as well!
public ListNode PrimerNodo
{
    get { return primerNodo; }
    set { primerNodo = value; }
}


回答4:

The problem is that your ListNode class is internal, while your List class is public. Code outside your .NET assembly can access the List class, but they can't use the PrimerNodo or UltimoNodo properties unless they can access the ListNode class too, so the compiler gives you that error.

If you want code outside of your assembly to be able to access ListNode, you can make it public. Otherwise, you can change the access modifier of the List class to internal or change the access modifier of the properties that use ListNode to private or internal.



回答5:

Make ListNode public:

namespace ClaseLista
{
   public class ListNode
   {
      //Constructor
      private object nombre;
      ..


回答6:

Your properties of type ListNode in the class ClaseLista are public but the ListNode type itself is not public. This creates mutually exclusive access control for the compiler. (After all, a consuming client can't very well use a property without being able to use the type for that property.)

For those properties to be public, the ListNode class also needs to be public.