Created
February 16, 2014 14:47
-
-
Save cbskgc/9035355 to your computer and use it in GitHub Desktop.
If you have a sealed class and would like to standardize or automate setup, one of the solutions is to create a class with an operator overload.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DefaultController : Controller | |
{ | |
public ActionResult Index() | |
{ | |
Response.Cookies.Add(new AccountIdCookie(12345)); | |
return View(); | |
} | |
} | |
class AccountIdCookie | |
{ | |
const string Name = "acct_id"; | |
int _accountId; | |
public AccountIdCookie(int accountId) | |
{ | |
_accountId = accountId; | |
} | |
public static implicit operator HttpCookie(AccountIdCookie accountIdCookie) | |
{ | |
return new HttpCookie(Name, accountIdCookie._accountId.ToString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment