本文整理汇总了C#中System.Collections.ArrayList.OfType方法的典型用法代码示例。如果您正苦于以下问题:C# ArrayList.OfType方法的具体用法?C# ArrayList.OfType怎么用?C# ArrayList.OfType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.ArrayList
的用法示例。
在下文中一共展示了ArrayList.OfType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OfTypeClause
public void OfTypeClause()
{
//OfType operator filters the collection based on the ability to cast to a specific type
IList mixedList = new ArrayList();
mixedList.Add(0);
mixedList.Add("One");
mixedList.Add("Two");
mixedList.Add(3);
mixedList.Add(new Student
{
StudentId = 1,
StudentName = "Bill"
});
//output string ony
var stringResult = from s in mixedList.OfType<string>()
select s;
//One
//Two
var studentOut = from s in mixedList.OfType<Student>()
select s;
var mthod = mixedList.OfType<Student>().ToList();
foreach (var student in mthod)
{
Console.WriteLine(student.StudentName);
}
}
示例2: M5
public void M5(ArrayList list)
{
foreach (A i in list.OfType<A>()) // Noncompliant
{ }
foreach (var i in list)
{ }
foreach (object i in list)
{ }
foreach (B i in list.OfType<B>()) // Noncompliant
{ }
}
示例3: CreateIDataParameterCollection
public static Mock<IDataParameterCollection> CreateIDataParameterCollection(this MockRepository repository)
{
var list = new ArrayList(); // ArrayList more closely matches IDataParameterCollection.
var parameters = repository.Create<IDataParameterCollection>();
parameters.Setup(p => p.Add(It.IsAny<IDataParameter>())).Returns((IDataParameter p) => list.Add(p));
parameters.Setup(p => p.Contains(It.IsAny<string>())).Returns((string n) => list.OfType<IDataParameter>().Any(p => p.ParameterName == n));
parameters.Setup(p => p[It.IsAny<int>()]).Returns((int i) => list[i]);
parameters.Setup(p => p[It.IsAny<string>()]).Returns((string n) => list.OfType<IDataParameter>().FirstOrDefault(p => p.ParameterName == n));
parameters.Setup(p => p.Count).Returns(() => list.Count);
return parameters;
}
示例4: LINQOverArrayList
static void LINQOverArrayList()
{
Console.WriteLine("***** LINQ over ArrayList *****");
// Here is a nongeneric collection of cars.
ArrayList myCars = new ArrayList() {
new Car{ PetName = "Henry", Color = "Silver", Speed = 100, Make = "BMW"},
new Car{ PetName = "Daisy", Color = "Tan", Speed = 90, Make = "BMW"},
new Car{ PetName = "Mary", Color = "Black", Speed = 55, Make = "VW"},
new Car{ PetName = "Clunker", Color = "Rust", Speed = 5, Make = "Yugo"},
new Car{ PetName = "Melvin", Color = "White", Speed = 43, Make = "Ford"}
};
// Transform ArrayList into an IEnumerable<T>-compatible type.
var myCarsEnum = myCars.OfType<Car>();
// Create a query expression.
//var fastCars = from c in myCarsEnum where c.Speed > 55 select c;
var fastCars = myCarsEnum.Where(c => c.Speed > 55).Select(c => c);
foreach (var car in fastCars)
{
Console.WriteLine("{0} is going too fast!", car.PetName);
}
}
示例5: OfTypeAsFilter
static void OfTypeAsFilter()
{
ArrayList myStaff = new ArrayList();
myStaff.AddRange(new object[] {10, 400, 8, false, new Car(), "string data"});
var myInts = myStaff.OfType<int>();
foreach (int i in myInts)
Console.WriteLine("Int value: {0}", i);
}
示例6: FilterTheShitOut
private void FilterTheShitOut()
{
ArrayList kitchenSink = new ArrayList();
kitchenSink.AddRange(new object[]{10, 20, "asdfa", true, false, 345.67, new ObjWithProps()});
IEnumerable<int> filteredInts = kitchenSink.OfType<int>();
foreach( int i in filteredInts )
Console.WriteLine(i);
}
示例7: OfTypeAsFilter
public static int OfTypeAsFilter() {
var myStuff = new ArrayList();
myStuff.AddRange(new object[] {15, false, new Car {PetName = "BMW", MaxSpeed = 260}, "str1", 559.3, 255, 42.0f, true, new Car("Toyouta", 230, 50) });
var myInts = myStuff.OfType<int>();
var myCars = myStuff.OfType<Car>();
var myDoubles = myStuff.OfType<double>();
var myFloats = myStuff.OfType<float>();
var myBools = myStuff.OfType<bool>();
myInts.PrintEnumerable();
myCars.PrintEnumerable();
myDoubles.PrintEnumerable();
myBools.PrintEnumerable();
myFloats.PrintEnumerable();
return 0;
}
示例8: OfTypeMethodTest
public void OfTypeMethodTest()
{
ArrayList fruits = new System.Collections.ArrayList();
fruits.Add("Mango");
fruits.Add("Orange");
fruits.Add("Apple");
fruits.Add(3.0);
fruits.Add("Banana");
// type을 지정해 그 type으로 cast 할 수 있는 element만 열거
IEnumerable<string> query = fruits.OfType<string>();
Assert.AreEqual(4, query.Count(), "3.0은 제외한 나머지 4개");
}
示例9: Main
static void Main()
{
ArrayList list = new ArrayList { "First", "Second", "Third"};
var strings = list.Cast<string>();
foreach (string item in strings)
{
Console.WriteLine(item);
}
list = new ArrayList { 1, "not an int", 2, 3};
var ints = list.OfType<int>();
foreach (int item in ints)
{
Console.WriteLine(item);
}
}
示例10: Test
public static void Test()
{
ArrayList list = new ArrayList();
list.Add("apple");
list.Add("orange");
list.Add(1);
list.Add(2);
var rst = list.OfType<string>();
foreach (var item in rst)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
示例11: QueryOverArrayList
public static int QueryOverArrayList() {
var myCars = new ArrayList() {
new Car{ PetName = "Henry", Color = "Silver", MaxSpeed = 100, Make = "BMW"},
new Car{ PetName = "Daisy", Color = "Tan", MaxSpeed = 90, Make = "BMW"},
new Car{ PetName = "Mary", Color = "Black", MaxSpeed = 55, Make = "VW"},
new Car{ PetName = "Clunker", Color = "Rust", MaxSpeed = 5, Make = "Yugo"},
new Car{ PetName = "Melvin", Color = "White", MaxSpeed = 43, Make = "Ford"}
};
var myCarsEnum = myCars.OfType<Car>();
var fastCars = from c in myCarsEnum where c.MaxSpeed > 55 select c;
fastCars.PrintEnumerable();
return 0;
}
示例12: LINQOverArrayList
static void LINQOverArrayList()
{
Console.WriteLine("***** LINQ at ArrayList *****\n");
ArrayList myCars = new ArrayList()
{
new Car{PetName = "Henry", Color = "Silver", Speed = 100, Make = "BMW"},
new Car{PetName = "Daisy", Color = "Tan", Speed = 90, Make = "BMW"},
new Car{PetName = "Mary", Color = "Black", Speed = 55, Make = "VW"},
new Car{PetName = "Clunker", Color = "Rust", Speed = 5, Make = "Yugo"},
new Car{PetName = "Melvin", Color = "White", Speed = 43, Make = "Ford"}
};
var myCarsEnum = myCars.OfType<Car>();
var fastCars = from c in myCarsEnum where c.Speed > 55 select c;
foreach (var car in fastCars)
Console.WriteLine("{0} is going too fast!", car.PetName);
}
示例13: Frm_Main_Load
private void Frm_Main_Load(object sender, EventArgs e)
{
ArrayList arrList = new ArrayList(); //创建动态数组
arrList.Add(1);//添加动态数组元素
arrList.Add(2);
arrList.Add("A");
arrList.Add(3);
arrList.Add("b");
//使用LINQ筛选动态数组中是string类型的元素
var query = from item in arrList.OfType<string>()
select item;
label1.Text = "是字符串类型的有:";//显示string类型的元素
foreach (var item in query)
{
label1.Text += item + " , ";
}
}
示例14: PopulateStockOutList
private object PopulateStockOutList(CoralPOS.Models.StockIn selectedStockIn)
{
var details = new ArrayList(StockOutDetails);
foreach (StockInDetail inDetail in selectedStockIn.StockInDetails)
{
Product product = inDetail.Product;
if (!ProductInStockOutList(details, product))
{
// create new stockout detail for that product
StockOutDetail newDetail = DataErrorInfoFactory.Create<StockOutDetail>();
newDetail.Product = inDetail.Product;
newDetail.ProductMaster = inDetail.ProductMaster;
newDetail.CreateDate = DateTime.Now;
newDetail.UpdateDate = DateTime.Now;
newDetail.CreateId = "admin";
newDetail.UpdateId = "admin";
newDetail.Quantity = inDetail.Quantity;
newDetail.StockQuantity = inDetail.Stock.Quantity;
newDetail.GoodQuantity = inDetail.Quantity;
details.Add(newDetail);
}
else
{
StockOutDetail result = (from sod in details.OfType<StockOutDetail>()
where sod.Product.ProductId.Equals(product.ProductId)
select sod).FirstOrDefault();
result.Quantity += inDetail.Quantity;
result.GoodQuantity += inDetail.Quantity;
}
}
StockOutDetails = details;
return null;
}
示例15: LoadBarcode
private object LoadBarcode()
{
if (ObjectUtility.LengthEqual(Barcode, 12))
{
ArrayList details = new ArrayList(StockOutDetails);
StockOutDetail result = (from sod in details.OfType<StockOutDetail>()
where sod.Product.ProductId.Equals(Barcode)
select sod).FirstOrDefault();
if (result != null) // if exist in list
{
result.Quantity += 1;
}
else
{
// get information from database
MainStock stock = MainStockLogic.FindByProductId(Barcode);
// create new stockout detail for that product
StockOutDetail newDetail = DataErrorInfoFactory.Create<StockOutDetail>();
newDetail.Product = stock.Product;
newDetail.ProductMaster = stock.ProductMaster;
newDetail.CreateDate = DateTime.Now;
newDetail.UpdateDate = DateTime.Now;
newDetail.CreateId = "admin";
newDetail.UpdateId = "admin";
newDetail.Quantity = 1;
newDetail.StockQuantity = stock.Quantity;
details.Add(newDetail);
}
StockOutDetails = details;
}
return null;
}