小编nan*_*nan的帖子

C#将<string>列表到带分隔符的字符串

C#中是否有一个函数可以快速将某些集合转换为字符串并使用分隔符分隔值?

例如:

List<string> names - > string names_together = "John, Anna, Monica"

c# string list delimiter

584
推荐指数
3
解决办法
52万
查看次数

一个Unicode字符占用多少字节?

我对编码有点困惑.据我所知,旧的ASCII字符每个字符占用一个字节.Unicode字符需要多少字节?

我假设一个Unicode字符可以包含来自任何语言的每个可能的字符 - 我是否正确?那么每个字符需要多少字节?

UTF-7,UTF-6,UTF-16等是什么意思?它们是不同版本的Unicode吗?

我阅读了有关Unicode维基百科文章,但这对我来说非常困难.我期待看到一个简单的答案.

language-agnostic string unicode encoding

224
推荐指数
8
解决办法
29万
查看次数

在c#中,"T:class"是什么意思?

在C#中where T : class意味着什么?

IE浏览器.

public IList<T> DoThis<T>() where T : class
Run Code Online (Sandbox Code Playgroud)

c# syntax

125
推荐指数
6
解决办法
9万
查看次数

C#字符串是否以空字符串结尾?

出于好奇只是一个简短的问题.

string str = "string";
Console.WriteLine(str.EndsWith(string.Empty));                  //true
Console.WriteLine(str.LastIndexOf(string.Empty) == str.Length); //false
//of course string are indexed from 0, 
//just wrote if for fun to check whether empty string get some extra index
///somehow by a miracle:)

//finally

Console.WriteLine(str.LastIndexOf(string.Empty) 
               == str.LastIndexOf('g'));                        //true :)
Run Code Online (Sandbox Code Playgroud)

.net c# string

18
推荐指数
2
解决办法
3155
查看次数

编译时和运行时转换c#

我想知道为什么在编译时检查C#中的某些转换,而在其他情况下,责任转储到CLR上.如上所述都是不正确的,但以不同的方式处理.

class Base { }
class Derived : Base { }
class Other { }

static void Main(string[] args)
{
    Derived d = (Derived)new Base();     //Runtime       InvalidCastException
    Derived d = (Derived)new Other();    //Compile-time  Cannot convert type...
}
Run Code Online (Sandbox Code Playgroud)

在阅读"C#深度"时,我发现了有关此主题的信息,其中autor说:
"如果编译器发现实际上不能使该转换工作,它将触发编译错误 - 如果理论上允许但实际上在执行时不正确,CLR会抛出异常."

"理论上"是否意味着通过继承层次结构(对象之间的另一个关联性?)连接,还是编译器的内部业务?

.net c# type-systems casting

17
推荐指数
1
解决办法
2821
查看次数

T的通用约束同时是引用类型和值类型?

我对理解泛型约束如何工作有一个问题.我想我在这里缺少一些重要的东西.我在评论中附上了我的问题,并希望提供一些解释.

//1st example:

class C <T, U>
    where T : class
    where U : struct, T
{
}
//Above code compiles well, 
//On first sight it looks like U might be reference type and value type
//at the same time. The only reason I can think of, is that T may be an 
//interface which struct can implement, Am I correct?

//2nd example

class CC<T, U>
    where T : class, new ()
    where U : struct, T
{
}

//I …
Run Code Online (Sandbox Code Playgroud)

c# generics constraints value-type reference-type

14
推荐指数
1
解决办法
4756
查看次数

重载索引器具有foreach'able类

我试图做这样的事情,但这不起作用:

    class Garage
    {
        private List<Car> cars = new List<Car>();

        public Car this[int i]
        {
            get { return cars[i]; }
        }
         //...
    }

       Garage g =  new Garage();
       //get CS1579 - no GetEnumerator definition
       foreach (Car c in g)
       {
           //...
       }
Run Code Online (Sandbox Code Playgroud)

由于MSDN说索引器可能超载,所以我决定在这里问专家.如何重载索引器与foreach循环合作?

.net c# foreach indexer

10
推荐指数
1
解决办法
5306
查看次数

如何隐藏界面的某些成员

我想创建一个实现的自定义集合ICollection.

但我不想暴露一些ICollection类似Clear方法的成员.

怎么做到这一点?

c# collections icollection

10
推荐指数
1
解决办法
1万
查看次数

泛型类中的方法重载

我正在使用在泛型类中包含以下重载方法的代码:

public class A<T>
{
    public void Process(T item) { /*impl*/ }
    public void Process(string item) { /*impl*/ }
}
Run Code Online (Sandbox Code Playgroud)

当参数化类时,string我是否失去了使用泛型参数调用版本的可能性?

var a = new A<string>();
a.Process(""); //Always calls the non-generic Process(string)
Run Code Online (Sandbox Code Playgroud)

c# generics overloading

10
推荐指数
3
解决办法
455
查看次数

Linq记忆问题

因为我对linq很新,所以我想在下面的例子中询问我的理解是否正确.

让我们假设我有非常大的动物名称集(100k记录),我想提交它们并以非常耗时的方法处理过滤的项目(2周).方法RunWithLinq()RunWithoutLinq()做法完全一样.

这是真的,使用第一种方法原始(大)集合将在离开方法后留在内存中,并且不会被触及GC,而使用无linq方法,集合将被删除GC

我会感激一点解释.

class AnimalProcessor
{
    private IEnumerable<string> animalsToProcess;
    internal AnimalProcessor(IEnumerable<string> animalsToProcess)
    {
        this.animalsToProcess = animalsToProcess;
    }
    internal void Start()
    {
        //do sth for 2 weeks with the collection
    }
}
class Program
{
    static void RunWithLinq()
    {
        var animals = new string[] { "cow", "rabbit", "newt", "ram" };
        var filtered = from animal in animals
                       where animal.StartsWith("ra")
                       select animal;
        AnimalProcessor ap = new AnimalProcessor(filtered);
        ap.Start();
    }
    static void RunWithoutLinq()
    { …
Run Code Online (Sandbox Code Playgroud)

c# linq memory-management

8
推荐指数
1
解决办法
3752
查看次数