BasicKnowledgeCSharp/LessonsAndTasks/Lesson 53 - var . Это не тип данных. Неявно типизированные переменные/Program.cs
Dvurechensky 058c8f2679 1.0
Main
2024-10-05 09:59:53 +03:00

71 lines
2.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
/*
* Ключевое слово var
*
* Неявно типизированные локальные переменные
*/
class Program
{
static int Sum_Int(int a, int b)
{
int aa = a + b;
return aa;
}
static int Sum_Var(int a, int b)
{
var aa = a + b;
return aa;
}
static void Main()
{
var u = 5;
// Когда полезен?
// 1 - сократить длинну кода
var dict = new Dictionary<int, string>();
var list = new List<int>();
// 2 - создать анонимный тип данных
var obj = new { Name = "Мартин", Age = 25 };
// 3 - использовать при выборке элементов в массиве
int[] numbers = { 1, 2, 3, 2, 4, 5 };
var result = from i in numbers where i > 1 select i;
foreach (var item in result)
Console.WriteLine($"{item}\t");
Stopwatch sw = Stopwatch.StartNew();
// 4 - когда нужно получить результат выполнения метода
for (int i = 0; i < int.MaxValue; i++)
{
var sum_int = Sum_Int(1, i);
}
sw.Stop();
Console.WriteLine($"Sum_Int {sw.ElapsedMilliseconds}");
sw.Restart();
for (int i = 0; i < int.MaxValue; i++)
{
var sum_var = Sum_Var(1, i);
}
sw.Stop();
Console.WriteLine($"Sum_Var {sw.ElapsedMilliseconds}");
/*
* Не может быть использовано параметром метода
* static void Bar (var a)
* Не может быть типом возвращаемого значения
* static var Foo()
* Не может быть полями класса
* class MyClass { public var a}
*/
Console.WriteLine();
Console.WriteLine(u.GetType());
Console.ReadKey();
}
}