Sunday, March 2, 2014

When should we use static members in a non-static class ?

at el-shitlony



Hello,


Sometimes the factory needs to call a method before it returns the instance.



void Main()
{
MyFactory factory = MyFactory.Create<MyFactory.Imp1>();
}

public abstract class MyFactory
{
public class Imp1 : MyFactory
{
protected override void Init()
{
}
}

public class Imp2 : MyFactory
{
protected override void Init()
{
}
}

public static MyFactory Create<T>() where T : MyFactory, new()
{
T imp = new T();

imp.Init();

return imp;
}

protected abstract void Init();
}

When you need to have one global copy of the code at all times it regardless to the amount of instances you make then it make sense to use statics.








at el-shitlony,


Your ode is just one more example of stupid C# code.


In other words, it is shitlony code, and not C# code.


You even don't know basic recommendations for using nested types,


How can shitlony like you be made moderator?...


The first C# principle is: KISS -> Keep It Simple Stupid .


Designing a nested type with oublic acessor is something for shitlony's....


See this code:



public abstract class MyFactory
{
// I suppose you have meaningful class elements in this abstract class.
}

public class Imp1 : MyFactory
{
public Imp1()
{
Init();
}
protected void Init()
{
}
}
public class Imp2 : MyFactory
{
public Imp2()
{
Init();
}
protected void Init()
{
}
}



This code is much better than yours because it's simpler: KISS.


I use Init() inside the ctor, because it's equivalent to your code.


Your code is just a convoluted form of something very simple, which exposes your arrogance, pretending to be smart...


Poor of those who don't see your stupidity...





In the beginning, there was nothing. Then the Big Bang! Now, look at me... I&#39;m ritehere.


No comments:

Post a Comment