//C# 7.3, .NET Framework 4.7.2
namespace
{
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
private static Random r = new Random();
public static void Main()
var Xi = RandomArray(10, () => r.Next(-10, 10));
Xi.Println();
var notZeroCount = Xi.Count(item => item != 0);
var replaced = Xi.Replace(item => item != 0, 1);
notZeroCount.Println();
replaced.Println();
}
private static T[] RandomArray<T>(int count, Func<T> next)
var temp = new T[count];
for(var i = 0; i < count; i++)
temp[i] = next();
return temp;
public static class Extensions
public static IEnumerable<T> Replace<T>(this IEnumerable<T> source, Func<T, bool> selector, T newValue)
foreach (var item in source)
if (selector(item))
yield return newValue;
else
yield return item;
public static int Count<T>(this IEnumerable<T> source, Func<T, bool> selector)
var count = 0;
if (selector(item)) count++;
return count;
public static void Println<T>(this T[] source)
Console.Write(item.ToString() + " ");
public static void Println<T>(this IEnumerable<T> source)
source.ToArray().Println();
public static void Println<T>(this T source)
Console.WriteLine(source);
//C# 7.3, .NET Framework 4.7.2
namespace
{
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
private static Random r = new Random();
public static void Main()
{
var Xi = RandomArray(10, () => r.Next(-10, 10));
Xi.Println();
var notZeroCount = Xi.Count(item => item != 0);
var replaced = Xi.Replace(item => item != 0, 1);
notZeroCount.Println();
replaced.Println();
}
private static T[] RandomArray<T>(int count, Func<T> next)
{
var temp = new T[count];
for(var i = 0; i < count; i++)
temp[i] = next();
return temp;
}
}
public static class Extensions
{
public static IEnumerable<T> Replace<T>(this IEnumerable<T> source, Func<T, bool> selector, T newValue)
{
foreach (var item in source)
{
if (selector(item))
yield return newValue;
else
yield return item;
}
}
public static int Count<T>(this IEnumerable<T> source, Func<T, bool> selector)
{
var count = 0;
foreach (var item in source)
if (selector(item)) count++;
return count;
}
public static void Println<T>(this T[] source)
{
foreach (var item in source)
Console.Write(item.ToString() + " ");
}
public static void Println<T>(this IEnumerable<T> source)
{
source.ToArray().Println();
}
public static void Println<T>(this T source)
{
Console.WriteLine(source);
}
}
}