You can use List.ConvertAll<int>:
List<int> ints = list2.ConvertAll<int>(int.Parse);
If you don't have a list you could use a Select with int.Parse:
List<int> ints = strings.Select(s=> int.Parse(s)).ToList();
Quick Notes for easy understanding. Chapters are on C#.Net,Linq,OOPS,Design Patterns,UML,Tools for development, Databases and many others. - Sid
You can use List.ConvertAll<int>:
List<int> ints = list2.ConvertAll<int>(int.Parse);
If you don't have a list you could use a Select with int.Parse:
List<int> ints = strings.Select(s=> int.Parse(s)).ToList();
Lazy<Fruit> lazyFruit = new Lazy<Fruit>();
Fruit fruit = lazyFruit.Value;
Fruit
class itself doesn't do anything here, The class variable _typesDictionary
is a Dictionary/Map used to store Fruit
instances by typeName
. using System;
using System.Collections;
using System.Collections.Generic;
public class Fruit
{
private string _typeName;
private static Dictionary<string, Fruit> _typesDictionary = new Dictionary<string, Fruit>();
private Fruit(String typeName)
{
this._typeName = typeName;
}
public static Fruit GetFruitByTypeName(string type)
{
Fruit fruit;
if (!_typesDictionary.TryGetValue(type, out fruit))
{
// Lazy initialization
fruit = new Fruit(type);
_typesDictionary.Add(type, fruit);
}
return fruit;
}
public static void ShowAll()
{
if (_typesDictionary.Count > 0)
{
Console.WriteLine("Number of instances made = {0}", _typesDictionary.Count);
foreach (KeyValuePair<string, Fruit> kvp in _typesDictionary)
{
Console.WriteLine(kvp.Key);
}
Console.WriteLine();
}
}
public Fruit()
{
// required so the sample compiles
}
}
class Program
{
static void Main(string[] args)
{
Fruit.GetFruitByTypeName("Banana");
Fruit.ShowAll();
Fruit.GetFruitByTypeName("Apple");
Fruit.ShowAll();
// returns pre-existing instance from first
// time Fruit with "Banana" was created
Fruit.GetFruitByTypeName("Banana");
Fruit.ShowAll();
Console.ReadLine();
}
}
using System.Collections.Generic;
namespace MyApplication {
class FooMultiton {
private static readonly Dictionary<object, FooMultiton> _instances = new Dictionary<object, FooMultiton>();
private FooMultiton() {
}
public static FooMultiton GetInstance(object key) {
lock (_instances) {
FooMultiton instance;
if (!_instances.TryGetValue(key, out instance)) {
instance = new FooMultiton();
_instances.Add(key, instance);
}
return instance;
}
}
}
}
Paste Here Your Source Code | ||||||||||||||
Source Code Formatting Options | ||||||||||||||
|
||||||||||||||
Copy Formatted Source Code | ||||||||||||||
Preview Of Formatted Code |