program.cs
来自「C#高级编程第6版随书源代码 值得下载」· CS 代码 · 共 72 行
CS
72 行
using System;
using System.Transactions;
namespace Wrox.ProCSharp.Transactions
{
class Program
{
static void Main()
{
UseTransactional();
}
private static void UseTransactional()
{
Transactional<int> intVal = new Transactional<int>(1);
Transactional<Student> student1 = new Transactional<Student>(new Student());
student1.Value.FirstName = "Andrew";
student1.Value.LastName = "Wilson";
Console.WriteLine("before the transaction, value: {0}", intVal.Value);
Console.WriteLine("before the transaction, student: {0}", student1.Value);
using (TransactionScope scope = new TransactionScope())
{
intVal.Value = 2;
Console.WriteLine("inside transaction, value: {0}", intVal.Value);
student1.Value.FirstName = "Ten";
student1.Value.LastName = "Sixty-Nine";
if (!Utilities.AbortTx())
scope.Complete();
}
Console.WriteLine("outside of transaction, value: {0}", intVal.Value);
Console.WriteLine("outside of transaction, student: {0}", student1.Value);
}
private static void IntDemo1()
{
Transactional<int> myint = new Transactional<int>(1);
Transactional<Student> myStudent = new Transactional<Student>();
myStudent.Value.FirstName = "Tom";
myStudent.Value.LastName = "Turbo";
myint.Value = 2;
using (TransactionScope scope = new TransactionScope())
{
Transactional<string> mystring = new Transactional<string>("one");
Console.WriteLine(myint.Value);
myint.Value = 5;
Console.WriteLine(myint.Value);
using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.RequiresNew))
{
Console.WriteLine(mystring);
Console.WriteLine(myint.Value);
scope2.Complete();
}
// scope.Complete();
}
Console.WriteLine(myint.Value);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?