Latest news for brand cialis for sale

Average Rating: 4.4 out of 5 based on 266 user reviews.

I find myself sometimes wishing I had a better switch/case construct in C#. Brand cialis for sale my requirements of "better" are:

  • case statements are automatically scoped
  • i can switch on more than simple types
  • i brand cialis for sale can have custom logic for my matching criteria

but why not just use if/else if/else. . . ?

Good question, I'm glad you asked! However, I don't have an excellent answer other than personal preference. if/else if/else is okay sometimes, but other times I have a deterministic set of conditions, and a switch type construct feels more symmetrical to me. I loves me some symmetric feeling code, it just feels cleaner somehow. So I'll present a somewhat contrived example just so you can see what the usage looks like, then we'll see the code.

Usage


public void DoSomething()
{
	object foo = new object();
	object bar = new object();
	object baz = new object();

	Switch<object>. On(baz)
		. Case(x => x. Equals(foo),  () =>
		{
			Console. WriteLine("came into foo case");
		})
		. Case(x => x. Equals(bar),  () =>
		{
			Console. WriteLine("came into bar case");
		})
		. Default( () =>
		{
			Console. WriteLine("came into default case");
		})
		. Go();
}

The output from the above, is as you'd expect, the default case, since baz wasn't equal to foo or bar.

Implementation


public class Switch<T>
{
	private T _target;
	private List<KeyValuePair<Predicate<T>,  Action>> _cases;
	private Action _default;

	public static Switch<T> On(T target)
	{
		return new Switch<T>(target);
	}

	public Switch(T target)
	{
		_target = target;
		_cases = new List<KeyValuePair<Predicate<T>,  Action>>();
	}

	public Switch<T> Case(Predicate<T> @case,  Action action)
	{
		_cases. Add(new KeyValuePair<Predicate<T>,  Action>(@case brand cialis for sale,  action));
		return this;
	}

	public Switch<T> Default(Action action)
	{
		_default = action;
		return this;
	}

	public void Go()
	{
		foreach(var @case in _cases)
			if (@case. Key(_target))
			{
				@case. Value();
				return;
			}
		if (_default != null)
			_default();
	}
}

The only part I don't really like is having to kick it off with the Go() at the end. Any ideas for a better way to kick it off, or at least a better name for it (instead of Go).

Feedback? Ideas?

Update

Paul had a great idea a couple great ideas (see comments) on how to get rid of the Go() at the end, and to force Default to only appear at the end. Here is the new implementation. Usage is the same, just remove [brand cialis for sale] the Go().


public class Switch<T>
{
	private T _target;
	private bool _alreadyMatched;

	public static Switch<T> On(T target)
	{
		return new Switch<T>(target);
	}

	public Switch(T target)
	{
		_target = target;
		_alreadyMatched = false;
	}

	public Switch<T> Case(Predicate<T> @case,  Action action)
	{
		if (!_alreadyMatched && @case(_target))
		{
			_alreadyMatched = true;
			action();
		}
		return this;
	}

	public void Default(Action action)
	{
		if (!_alreadyMatched)
		{
			_alreadyMatched = true;
			action();
		}
		return this;
	}
}


?? 2008-2016 Legit Express Chemist.