C# 我如何洗牌我的字符串列表?

C# 我如何洗牌我的字符串列表?,c#,C#,可能重复: 我以为我的代码可以工作,但现在看来不行了。以下是我所拥有的: public class NoteDetail { public NoteDetail() { _noteDetails = new List<string>(); } public IList<string> NoteDetails { get { return _noteDetails; } } private readonly List

可能重复:

我以为我的代码可以工作,但现在看来不行了。以下是我所拥有的:

public class NoteDetail
{
    public NoteDetail()
    {
        _noteDetails = new List<string>();
    }
    public IList<string> NoteDetails { get { return _noteDetails; } }
    private readonly List<string> _noteDetails;
}
现在我想洗牌,所以我用了这个套路:

    public static void ShuffleGenericList<T>(IList<T> list)
    {
        //generate a Random instance
        var rnd = new Random();
        //get the count of items in the list
        var i = list.Count();
        //do we have a reference type or a value type
        T val = default(T);

        //we will loop through the list backwards
        while (i >= 1)
        {
            //decrement our counter
            i--;
            //grab the next random item from the list
            var nextIndex = rnd.Next(i, list.Count());
            val = list[nextIndex];
            //start swapping values
            list[nextIndex] = list[i];
            list[i] = val;
        }
    }
publicstaticvoidshufflegenericlist(IList列表)
{
//生成一个随机实例
var rnd=新随机数();
//获取列表中项目的计数
var i=list.Count();
//我们有引用类型还是值类型
T val=默认值(T);
//我们将向后循环浏览列表
而(i>=1)
{
//减少我们的计数器
我--;
//从列表中抓取下一个随机项
var nextIndex=rnd.Next(i,list.Count());
val=列表[nextIndex];
//开始交换值
列表[nextIndex]=列表[i];
列表[i]=val;
}
}
我的问题是我不知道如何洗牌。我尝试了以下方法,但它给出了:

错误237参数1:无法从“System.Collections.Generic.IList”转换为“System.Collections.Generic.IList”

Sort.ShuffleGenericList(noteDetail.NoteDetails);

谁能看出我做错了什么。我觉得一切正常,我不明白为什么会出现这个错误:-(

您应该更改这个:

Sort.ShuffleGenericList<NoteDetail>(noteDetail.NoteDetails);
Sort.ShuffleGenericList(noteDetail.NoteDetails);
致:

Sort.ShuffleGenericList(noteDetail.NoteDetails);

因为
noteDetail.NoteDetails
是一个
列表
,而不是
列表

您使用了错误的类型来参数化您的泛型方法,请改为执行以下操作:

Sort.ShuffleGenericList(noteDetail.NoteDetails);
或更明确(但不必要):

Sort.ShuffleGenericList(noteDetail.NoteDetails);

您将
notedeail
作为类型传递,而不是
string
-这不起作用。

我将您的代码放入VS中。只需稍作修改即可:

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

namespace MsgBaseSerializeationTest
{
    class StackOverflow
    {
        public void Test()
        {
            var noteDetail = new NoteDetail<string>();
            noteDetail.NoteDetails.Add("aaa");
            noteDetail.NoteDetails.Add("bbb");
            noteDetail.NoteDetails.Add("ccc");

            NoteDetail<string>.ShuffleGenericList(noteDetail);
        }

    }


    public class NoteDetail<T> : List<T>
    {
        public NoteDetail()
        {
            _noteDetails = new List<string>();
        }
        public IList<string> NoteDetails { get { return _noteDetails; } }
        private readonly List<string> _noteDetails;

        public static void ShuffleGenericList(IList<T> list)
        {
            //generate a Random instance
            var rnd = new Random();
            //get the count of items in the list
            var i = list.Count();
            //do we have a reference type or a value type
            T val = default(T);

            //we will loop through the list backwards
            while (i >= 1) {
                //decrement our counter
                i--;
                //grab the next random item from the list
                var nextIndex = rnd.Next(i, list.Count());
                val = list[nextIndex];
                //start swapping values
                list[nextIndex] = list[i];
                list[i] = val;
            }
        }


    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
命名空间MSGBaseSerializationTest
{
类堆栈溢出
{
公开无效测试()
{
var notetail=新的notetail();
noteDetail.NoteDetails.添加(“aaa”);
noteDetail.NoteDetails.Add(“bbb”);
注详情。注详情。添加(“ccc”);
NoteDetail.ShuffleGenericList(NoteDetail);
}
}
公共类NoteDetail:列表
{
公共说明详情()
{
_noteDetails=新列表();
}
公共IList NoteDetails{get{return\u NoteDetails;}}
私有只读列表_noteDetails;
公共静态无效ShuffleGenericList(IList列表)
{
//生成一个随机实例
var rnd=新随机数();
//获取列表中项目的计数
var i=list.Count();
//我们有引用类型还是值类型
T val=默认值(T);
//我们将向后循环浏览列表
而(i>=1){
//减少我们的计数器
我--;
//从列表中抓取下一个随机项
var nextIndex=rnd.Next(i,list.Count());
val=列表[nextIndex];
//开始交换值
列表[nextIndex]=列表[i];
列表[i]=val;
}
}
}
}

难道没有一个完整的主题来说明这种天真的做法是如何错误的吗?让框架发挥它的魔力吧:Sort.ShuffleGenericList(notedail.notedails);或者干脆
Sort.ShuffleGenericList(notedail.notedails)
Sort.ShuffleGenericList<string>(noteDetail.NoteDetails);
Sort.ShuffleGenericList(noteDetail.NoteDetails);
Sort.ShuffleGenericList<string>(noteDetail.NoteDetails);
using System;
using System.Collections.Generic;
using System.Linq;

namespace MsgBaseSerializeationTest
{
    class StackOverflow
    {
        public void Test()
        {
            var noteDetail = new NoteDetail<string>();
            noteDetail.NoteDetails.Add("aaa");
            noteDetail.NoteDetails.Add("bbb");
            noteDetail.NoteDetails.Add("ccc");

            NoteDetail<string>.ShuffleGenericList(noteDetail);
        }

    }


    public class NoteDetail<T> : List<T>
    {
        public NoteDetail()
        {
            _noteDetails = new List<string>();
        }
        public IList<string> NoteDetails { get { return _noteDetails; } }
        private readonly List<string> _noteDetails;

        public static void ShuffleGenericList(IList<T> list)
        {
            //generate a Random instance
            var rnd = new Random();
            //get the count of items in the list
            var i = list.Count();
            //do we have a reference type or a value type
            T val = default(T);

            //we will loop through the list backwards
            while (i >= 1) {
                //decrement our counter
                i--;
                //grab the next random item from the list
                var nextIndex = rnd.Next(i, list.Count());
                val = list[nextIndex];
                //start swapping values
                list[nextIndex] = list[i];
                list[i] = val;
            }
        }


    }
}