generics

Angelika Langer GenericsFAQ - Can i use raw type as any other type?

本秂侑毒 提交于 2020-02-04 05:27:05
问题 I am reading through documentation of AgelikaLangerDoc. I have just started reading this article. I have seen the following syntax in the code. interface Copyable<T>{ T copy(); } final class Wrapped<Elem extends Copyable<Elem>>{ } I am not able to understand the following generics syntax meaning. <Elem extends Copyable<Elem>> I know its pretty basic but really generics is tricky. Why do we need to put it like this? We could have said something ike final class Wrapped<Copyable<Elem>>{ } I know

Angelika Langer GenericsFAQ - Can i use raw type as any other type?

老子叫甜甜 提交于 2020-02-04 05:26:09
问题 I am reading through documentation of AgelikaLangerDoc. I have just started reading this article. I have seen the following syntax in the code. interface Copyable<T>{ T copy(); } final class Wrapped<Elem extends Copyable<Elem>>{ } I am not able to understand the following generics syntax meaning. <Elem extends Copyable<Elem>> I know its pretty basic but really generics is tricky. Why do we need to put it like this? We could have said something ike final class Wrapped<Copyable<Elem>>{ } I know

What does class? (class with question mark) mean in a C# generic type constraint?

核能气质少年 提交于 2020-02-04 01:47:07
问题 While I tried to find an answer to another question I noticed that this code compiles in C#: public void Foo<T>(T obj) where T : class? { } I did not manage to find in the documentation what it even means. 回答1: It enforces that T has to be a nullable reference type. The type you set in for T, must derive from object? . It's a new feature in C#8, to explictly declare a type as nullable. if you have Add<T>(T tmp); You document, it's OK to Add null; 回答2: To allow your class return null value

What does class? (class with question mark) mean in a C# generic type constraint?

血红的双手。 提交于 2020-02-04 01:46:53
问题 While I tried to find an answer to another question I noticed that this code compiles in C#: public void Foo<T>(T obj) where T : class? { } I did not manage to find in the documentation what it even means. 回答1: It enforces that T has to be a nullable reference type. The type you set in for T, must derive from object? . It's a new feature in C#8, to explictly declare a type as nullable. if you have Add<T>(T tmp); You document, it's OK to Add null; 回答2: To allow your class return null value

Odd Generic Inheritance pattern

梦想与她 提交于 2020-02-03 17:15:11
问题 During some research, I ran into an inheritance pattern using generics I have not seen before. http://thwadi.blogspot.ca/2013/07/using-protobuff-net-with-inheritance.html public abstract class BaseClass<TClass> where TClass : BaseClass<TClass> { //... } public class DerivedClass : BaseClass<DerivedClass> { //... } Usage: static void Main(string[] args) { DerivedClass derivedReference = new DerivedClass(); //this looks odd... BaseClass<DerivedClass> baseReference = derivedReference; //this

Odd Generic Inheritance pattern

元气小坏坏 提交于 2020-02-03 17:15:07
问题 During some research, I ran into an inheritance pattern using generics I have not seen before. http://thwadi.blogspot.ca/2013/07/using-protobuff-net-with-inheritance.html public abstract class BaseClass<TClass> where TClass : BaseClass<TClass> { //... } public class DerivedClass : BaseClass<DerivedClass> { //... } Usage: static void Main(string[] args) { DerivedClass derivedReference = new DerivedClass(); //this looks odd... BaseClass<DerivedClass> baseReference = derivedReference; //this

How to borrow the T from a RefCell<T> as a reference?

坚强是说给别人听的谎言 提交于 2020-02-03 15:25:50
问题 Sometimes I have a struct containing a value which is wrapped in a RefCell , and I want to borrow the value, but I don't want to make the signature of the accessor function to depend on the internal implementation. To make it work, I need to return the reference as a Ref<T> instead of a &T . For example, if this is my struct: use std::cell::RefCell; pub struct Outer<T> { inner: RefCell<T>, } I could write an accessor like this: use std::cell::Ref; impl<T> Outer<T> { fn get_inner_ref(&self) ->

How to borrow the T from a RefCell<T> as a reference?

孤者浪人 提交于 2020-02-03 15:25:28
问题 Sometimes I have a struct containing a value which is wrapped in a RefCell , and I want to borrow the value, but I don't want to make the signature of the accessor function to depend on the internal implementation. To make it work, I need to return the reference as a Ref<T> instead of a &T . For example, if this is my struct: use std::cell::RefCell; pub struct Outer<T> { inner: RefCell<T>, } I could write an accessor like this: use std::cell::Ref; impl<T> Outer<T> { fn get_inner_ref(&self) ->

How to borrow the T from a RefCell<T> as a reference?

孤者浪人 提交于 2020-02-03 15:24:26
问题 Sometimes I have a struct containing a value which is wrapped in a RefCell , and I want to borrow the value, but I don't want to make the signature of the accessor function to depend on the internal implementation. To make it work, I need to return the reference as a Ref<T> instead of a &T . For example, if this is my struct: use std::cell::RefCell; pub struct Outer<T> { inner: RefCell<T>, } I could write an accessor like this: use std::cell::Ref; impl<T> Outer<T> { fn get_inner_ref(&self) ->

Cannot implicity convert type System.Collections.Generic.IEnumerable<B> to System.Collections.Generic.List<B>

[亡魂溺海] 提交于 2020-02-03 13:24:46
问题 With the code below i get this error and need help with how to let method Load return List<B> Cannot implicity convert type System.Collections.Generic.IEnumerable to System.Collections.Generic.List public class A { public List<B> Load(Collection coll) { List<B> list = from x in coll select new B {Prop1 = x.title, Prop2 = x.dept}; return list; } } public class B { public string Prop1 {get;set;} public string Prop2 {get;set;} } 回答1: Your query returns an IEnumerable , while your method must