📜 ⬆️ ⬇️

Everything new in C # 3.0 in one small program

On the street, the weather is not very good, not to go anywhere, I decided to create something perverted, I really wanted to ... I offer the code for a small program below. What should she give to the console? Compile and test yourself ...

For those who lower karma: do not like the post, go to other topics. He is in a personal blog. I want to comment, but I do not create virtuals, do not be so harsh ...



using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
')
namespace Frozik
{
static class Program
{
public static char FirstChar <TInput> ( this TInput value )
where TInput: class
{
return value.ToString () [0];
}

static void Main ()
{
var arguments = new List < string > { "arg_0" , "temp_z" , "arg_1" , "arg_2" };

object locker;

lock (locker = new object ())
{
Console.WriteLine ( "Started" );

(( Func < string >) (() =>
{
lock (locker)
{
var result = string .Empty;

foreach ( string arg in
(from argEntity in arguments
where argEntity.StartsWith ( "arg_" )
select argEntity.Substring (4)))
{
Console.WriteLine (result + = arg.FirstChar < string > ());
}

return result;
}
})). BeginInvoke (
( delegate (IAsyncResult result) {
Console.WriteLine (
new
{
Lenght =
(( Func < string >) ((AsyncResult) result) .AsyncDelegate).
EndInvoke (result) .Length
} .Lenght);
})
new object ()
);

Console.WriteLine ( "Finished" );

Console.ReadKey ();
}

Console.ReadKey ();
}
}
}

Below are explanations for those who do not understand the syntax


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;

namespace Frozik
{
static class Program
{
// Create an extension for all reference types, in my program extension
// will be used only for string'ov. My extension is generic only to
// to confuse more.
// How extensions work:
// You can add methods to any standard and non-standard type as if they were
// were implemented in the class itself. Further in the program I use the extension
// for the string - arg.FirstChar <string> ()
// Extensions must be public static and created in a static class
public static char FirstChar <TInput> (this TInput value)
// The extension will be for reference types - generic types are available starting from C # 2.0 and .Net 2.0
where TInput: class
{
return value.ToString () [0];
}

static void Main ()
{
// Innovation - for the place to call the Add method, you can simply specify all the data
// when declaring an array. Also in this case, the arguments will be of type List <string>,
// intellisense will understand this and will work correctly.
var arguments = new List <string> {"arg_0", "temp_z", "arg_1", "arg_2"};

object locker;

lock (locker = new object ())
{
Console.WriteLine ("Started");

// Lambda expression, essentially an anonymous delegate. In the standard type
// Func (function with return parameter) you can also specify input parameters
// which are not needed in my example
// Anonymous delegate could be created for the place of lambda expression
// (delegate () {
((Func <string>) (() =>
// If there was a task, we could write the following:
// Console.WriteLine (((Func <int, int, string>) ((x, y) => (x + y) .ToString ())) (5, 5));
// We would create a lambda expression that has 2 parameters of type int as input, and output as output
// string, and would call it with parameters 5 and 5. You can try, it will output 10 to the console
// The example in the comment above is implemented using an anonymous delegate -
// Console.WriteLine (((Func <int, int, string>) (delegate (int x, int y) {return (x + y) .ToString ();))) (5, 5));
{
// Lambda expression has access to local variables that were
// defined in the parent method.
lock (locker)
{
// In this case, the result will be of type string
var result = string.Empty;

foreach (string arg in
// Expression Linq. The result will be - IEnumerable <string>
// We are looking for in arguments all values ​​that begin with "arg_"
// and as a result we take only Substring (4) from each one found
// result. IEnumerable <string> is easy to convert to List <string> or
// string [], simply add to the end .ToList () or ToArray ()
// Also, this expression is similar to the following -
// arguments.Where ((argEntity) => argEntity.StartsWith ("arg_")).
// Select ((argEntity) => argEntity.Substring (4))
// You can see in more detail here - msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx
(from argEntity in arguments
where argEntity.StartsWith ("arg_")
select argEntity.Substring (4)))
{
// Add the first letters of the lines and return the result of the addition
Console.WriteLine (result + = arg.FirstChar <string> ());
}

return result;
}
})).
// Since the lambda expression can be an anonymous delegate, then we can call it
// in a separate thread
BeginInvoke (
// Create an anonymous delegate to be called when the request is executed,
// for the place of this one could write a lambda expression
// (result) => {
(delegate (IAsyncResult result) {
Console.WriteLine (
// Create a type on the fly that has one Length property,
// is present here only to confuse more
new
{
Lenght = ((Func <string>) ((AsyncResult) result) .AsyncDelegate).
EndInvoke (result) .Length
}.
// Get the Lenght property of an anonymous type
Lenght);
})
new object ()
);

Console.WriteLine ("Finished");

Console.ReadKey ();
}

Console.ReadKey ();
}
}
}

// There are only locks left, but I will not explain them, everyone should know that ;-)

Source: https://habr.com/ru/post/22199/


All Articles