c# interface for static that create on the class? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i have this class
class Class1
{
public static List<int> abc = new List<int>();
}
so now i can access from outside the class :
public void x()
{
Class1.abc.Add(2);
}
what i want to do is :
I want "Class1" implement from INTERFACE and implement this "abc"
that i can do Class1.abc.Add(2);
i mean that the abc will be on interface..
I already try to do this but i not without any success
how can i do this please?
thanks!

As others have said, only objects can implement interfaces in C#. See Why Doesn't C# Allow Static Methods to Implement an Interface? for a good explanation.
Instead, you could use the Factory (an object that use used to create other objects) or Singleton patterns (use a single instance of an object). These could implement an interface including the "Add" method you mentioned.
For example, instead of:
class Class1
{
public static List<int> abc = new List<int>();
}
Class1.abc.Add(1); // Add numbers
... have something like ...
interface IListInterface
{
List<int> List;
}
class Lists: IListInterface
{
public Lists()
{
List = new List<int>();
}
public List<int> List
{
get;
}
}
// Using the above
public void AddToList(IListInterface lists, int a)
{
lists.List.Add(a);
}
This standardize your access to the lists by using an interface and allows you to swap the implementation of the list interface without affecting the using code, useful for automated testing.

Related

Class per command or not - best practices [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 months ago.
Improve this question
Let's say that I have two commands CreateCarCommand and UpdateCarCommand and they obviously share some properties.
What is the best practice, should I create separate classes per command like
public class CreateCarCommand { ... }
public class UpdateCarCommand { ... }
or should UpdateCarCommand inherit from CreateCarCommand
public class CreateCarCommand { ... }
public class UpdateCarCommand : CarCommand { ... }
I would have two separate command classes - and if you have a lot of common properties, use a CarCommandBase base class and make CreateCarCommand and UpdateCarCommand inherit from that base class:
public class CarCommandBase
{
// define common properties and possibly methods here
}
public class CreateCarCommand : CarCommandBase
{
// custom code
}
public class UpdateCarCommand : CarCommandBase
{
// custom code
}

How can I create a reference that can contain both a stack and a list without needing a cast in c#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'd like to be able to have a reference to a collection in a superclass that will need to contain either a stack or a list in a subclass, and I am having trouble understanding how to make that work, any ideas?
Something like:
public class Group
{
Collection<Human> group;
}
public class PeopleStack : Group
{
public PeopleStack()
{
this.group = new Stack<Human>();
}
}
public class Crowd : Group
{
public Crowd()
{
this.group = new List<Human>();
}
}
The Stack is defined as
public class Stack<T> :
System.Collections.Generic.IEnumerable<T>,
System.Collections.Generic.IReadOnlyCollection<T>,
System.Collections.ICollection
and the List is
public class List<T> :
System.Collections.Generic.ICollection<T>,
System.Collections.Generic.IEnumerable<T>,
System.Collections.Generic.IList<T>,
System.Collections.Generic.IReadOnlyCollection<T>,
System.Collections.Generic.IReadOnlyList<T>,
System.Collections.IList
So you can use one of the strong (generic) types IEnumerable<Human> or IReadOnlyCollection<Human> for group.
And of course that should be protected etc, but I suppose you know that.

Why would an empty class serve as the base class for several generic classes? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm looking over some scripts from a Unity project called 2D GameKit, & I'm trying to learn from piecing together the purpose of each. In one of them that serves as a mechanism for persistent storage, I noticed that several generic classes were derived from an empty base class. Why?
Here is a sample of the code:
public class Data
{
}
public class Data<T> : Data
{
public T value;
public Data(T value)
{
this.value = value;
}
}
public class Data<T0, T1> : Data
{
public T0 value0;
public T1 value1;
public Data(T0 value0, T1 value1)
{
this.value0 = value0;
this.value1 = value1;
}
}
So that you can check (know / compare) that a certain object is definitely data. If you don't have a base class, when you want to check if an object is certainly a type of data before processing it, you will need to compare with Data<T> AND Data<T0, T1> and also any other data types that is created in the future

Can I cast abstract and interface classes (If i can than how will be cast)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to verify whether can i cast abstract and interface class . I have tried the following but can't get it so I assume I'm going about it the wrong way.
Your question is a bit vague. There can be quite a few possibilities to your question. I have tried to give them here. See if it fits your needs. If not then please comment
Building of class structure
public abstract class MyAbClass
{
public abstract void MyM1();
}
public interface IMyInterface
{
void MyM2();
}
public class MyConcretClass1 : MyAbClass
{
public override void MyM1()
{
//Your implementation here
}
}
public class MyConcretClass2 : IMyInterface
{
public void MyM2()
{
//Your implementation here
}
}
Possibilities
//Following are the possibilities
//This can be done in some other class and passed to some other method
//maybe a factory pattern
MyAbClass cls1 = new MyConcretClass1();
//Will call method of MyConcretClass1
cls1.MyM1();
if (cls1 is MyConcretClass1)
{
//Do casting here
}
var cls2 = cls1 as MyConcretClass1;
if (cls2 != null)
{
//Do your stuff here
}
//This can be done in some other class and passed to some other method
//maybe a factory pattern
IMyInterface cls3 = new MyConcretClass2();
//Will call method of MyConcretClass2
cls3.MyM2();
if (cls3 is MyConcretClass2)
{
//Do casting here
}
var cls4 = cls3 as MyConcretClass2;
if (cls4 != null)
{
//Do your stuff here
}
Hope this helps

How can i create dynamic variables on the fly in c# using Reflection? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm creating a simulator of ecosystems where species can be used to simulate various diseases, my problem is that I start using 4 species but if I need more ... I need more variables to store, my question is, Is there any way through Reflection to let me create dynamic variables during the execution of an event in my program? Thank you! i'm using Windows Presentation Foundation and C#
The normal way to handle this is to have a base class for your disease species and then use a collection to hold them all:
public abstract class DiseaseBase
{
public abstract void Spread();
}
public class Anthrax : DiseaseBase
{
public override void Spread()
{
GetPostedToPolitician();
}
}
public class BirdFlu : DiseaseBase
{
public override void Spread()
{
Cluck();
SneezeOnHuman();
}
}
public class SwineFlu : DiseaseBase
{
public override void Spread()
{
//roll in mud around other piggies
}
}
public class ManFlu : DiseaseBase
{
public override void Spread()
{
//this is not contagious
//lie in bed and complain
//get girlfriend to make chicken soup
//serve chicken soup with beer and baseball/football/[A-Za-z0-9]+Ball
}
}
public List<DiseaseBase> DiseaseCollection = new List<Disease>();
So everything gets stored in the collection as the base class (DiseaseBase), and with the appropriate use of abstract methods in the base and/or interfaces you can always handle each disease instance as the base object.

Categories

Resources