C#中是否有一个函数可以快速将某些集合转换为字符串并使用分隔符分隔值?
例如:
List<string> names - > string names_together = "John, Anna, Monica"
我对编码有点困惑.据我所知,旧的ASCII字符每个字符占用一个字节.Unicode字符需要多少字节?
我假设一个Unicode字符可以包含来自任何语言的每个可能的字符 - 我是否正确?那么每个字符需要多少字节?
UTF-7,UTF-6,UTF-16等是什么意思?它们是不同版本的Unicode吗?
在C#中where T : class意味着什么?
IE浏览器.
public IList<T> DoThis<T>() where T : class
Run Code Online (Sandbox Code Playgroud) 出于好奇只是一个简短的问题.
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) 我想知道为什么在编译时检查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会抛出异常."
"理论上"是否意味着通过继承层次结构(对象之间的另一个关联性?)连接,还是编译器的内部业务?
我对理解泛型约束如何工作有一个问题.我想我在这里缺少一些重要的东西.我在评论中附上了我的问题,并希望提供一些解释.
//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) 我试图做这样的事情,但这不起作用:
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循环合作?
我想创建一个实现的自定义集合ICollection.
但我不想暴露一些ICollection类似Clear方法的成员.
怎么做到这一点?
我正在使用在泛型类中包含以下重载方法的代码:
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) 因为我对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# ×9
.net ×3
string ×3
generics ×2
casting ×1
collections ×1
constraints ×1
delimiter ×1
encoding ×1
foreach ×1
icollection ×1
indexer ×1
linq ×1
list ×1
overloading ×1
syntax ×1
type-systems ×1
unicode ×1
value-type ×1