اثبات قانون مشاهدهگر در برنامه نویسی
نویسنده: میثم نوایی
تاریخ: ۱۳۹۳/۰۷/۲۷ ۱:۳۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Person
{
public Person()
{
personId= this.GetType().Name + (new Random()).Next(1, int.MaxValue);
}
} var student1=new Student(){Name="Iraj",Age=21};
var student1=new Student(){Name="Nima",Age=20};
var student1=new Student(){Name="Sara",Age=25};
var student1=new Student(){Name="Mina",Age=22};
var student1=new Student(){Name="Narges",Age=26};
var teacher1=new Student(){Name="Navaei",Age=45};
var teacher2=new Student(){Name="Imani",Age=50}; public class Person
{
public Person()
{
personId= this.GetType().Name + (new Random()).Next(1, int.MaxValue);
Debug.Print(personId)
}
} using System;
using System.Threading;
public static class RandomProvider
{
private static int seed = Environment.TickCount;
private static ThreadLocal<Random> randomWrapper = new ThreadLocal<Random>(() =>
new Random(Interlocked.Increment(ref seed))
);
public static Random GetThreadRandom()
{
return randomWrapper.Value;
}
} class Program
{
static void Main(string[] args)
{
var lst = new List<Person>();
Random rnd = new Random();
for (int i = 0; i < 50; i++)
{
lst.Add(new Person(rnd));
}
foreach (var item in lst)
{
Console.WriteLine(item.PersonId);
}
Console.ReadKey();
}
}
public class Person
{
public Person(Random rnd)
{
PersonId = this.GetType().Name + rnd.Next(1, int.MaxValue);
}
public string PersonId { get; set; }
}