[Solved] How to optimize redundant code in c#?

It is not possible to put generic constraint about specific constructor availabilty, so you cannot guarantee inside the method that TDal _dal = new TDal(_connectionString); is possible. I would refactor it then to provide dal externally: public List<TRule> getData<TRule>(TRule perRA, IDal<TRule> dalRA, int acao) { List<TRule> list = new List<TRule>(); try { list = dalRA.getDATA(perRA, … Read more

[Solved] C# method signatures – restricting types – what’s the correct terminology? [closed]

I think the term you are looking for is generic type constraints. From the linked MSDN article: When you define a generic class, you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates your class. If client code tries to instantiate your class by using … Read more

[Solved] C# generic inheritance and covariance part 2

There does not appear to be any question in this question, so I’ll make up a few questions to answer. What is a covariant conversion? Let’s suppose we have some types Fruit and Apple and Banana with the obvious relationships; Apple is a kind of Fruit, and so on. A covariant conversion is one where … Read more

[Solved] Java Generics Question

That ‘terminated’ you have been seeing lately is the expected behavior when your program finishes. Put some System.outs or asserts to verify that your code runs (here it runs, with some awful cast warnings, but runs) final Queue12<T> ringBuffer = new QueueImpl12<T>(); T o = (T) new String(“this”); ringBuffer.enqueue(o); //add element to the back System.out.println(ringBuffer.peek());//this … Read more

[Solved] Generic class of one type to another type in auto mapper

For Mapper.Map<PagedList<CasteModel>>(result);, you need to initialize Mapper like below in Startup.cs public void ConfigureServices(IServiceCollection services) { Mapper.Initialize(cfg => { cfg.AddProfile<AppProfile>(); }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); } But, it it recommended to use Dependence Injection to resolve Mapper. Install Package AutoMapper.Extensions.Microsoft.DependencyInjection Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddAutoMapper(typeof(Startup)); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); } UseCase public class ValuesController : ControllerBase { private readonly … Read more

[Solved] Java generic wildcard function

class Super{ void superMethod(){} } class Sub extends Super{ void subMethod(){} } class Sub2 extends Super{ void subMethod2(){} } static <T extends Super> void processSuper(T input){ // this is safe and compiles input.superMethod(); // this doesn’t compile input.subMethod(); // nor this input.subMethod2(); } Look at the above code snippet. When we use an extends Bound, … Read more

[Solved] If abstract base class contains parameterised constructor (and derived does not) why can’t it be used? [duplicate]

No, you can’t. It’s a limitation that says ‘each derived class should use (implicitly or explicitly) at least one constructor from base class. In your example, your child class implicitly has parameterless constructor which implicitly uses parameterless constructor from base. So, you need to: either setup parameretised constructor in every derived class or delele this … Read more

[Solved] If abstract base class contains parameterised constructor (and derived does not) why can’t it be used? [duplicate]

Introduction Abstract base classes are a powerful tool in object-oriented programming, allowing for the creation of a base class that can be extended by derived classes. However, if an abstract base class contains a parameterized constructor, and the derived class does not, it can be difficult to use the abstract base class. This is because … Read more

[Solved] How to sort a list of int list

You can do this var results = numberLists.OrderBy(x => x[0]) .ThenBy(x => x[1]) .ThenBy(x => x[2]); foreach (var result in results) { foreach (var subresult in result) { Console.Write(subresult + ” “); } Console.WriteLine(); } Output 2 3 9 2 4 7 4 7 8 6 8 9 Full Demo here Additional Results Enumerable.OrderBy Method … Read more

[Solved] What is generics, its uses and application? [duplicate]

Generics is used to create “Type safe” collections like List, Map etc. It means that we can add only known/expected types to collections in order to avoid storing different data in one collection. For e.g //without generics ArrayList list = new ArrayList(); list.add(new Object()); list.add(new Interger()); list.add(new String()); //with generics ArrayList<String> list = new ArrayList<String>(); … Read more