Ведь c# - это компилируемый язык а не интерпретируемый, а значит код не читается строчка за строчкой а строится синтаксическое дерево. И поэтому мне интересно не все ли равно где объявлять пространства имен?
Dictionary<string, string[]> wordDictionary = new Dictionary<string, string[]>();
wordDictionary.Add("YANDEX", new string[] {'yandex1','yandex2'});
wordDictionary.Add("GOOGLE", new string[] {'google1','google2'});
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
List<UserInfo> my_list = new List<UserInfo>();
my_list.Add(new UserInfo { name = "Вася", age = 19, male = true});
my_list.Add(new UserInfo { name = "Маша", age = 23, male = false});
Dictionary<string, UserInfo> dic = new Dictionary<string, UserInfo>();
dic.Add("YAndeX", my_list[0]);
dic.Add("GooGle", my_list[1]);
foreach (var temp in dic)
{
Console.WriteLine("Key: " + temp.Key + " Value: Age" + temp.Value.age + " Name: " + temp.Value.name + " is male: " + temp.Value.male);
}
Console.ReadLine();
}
}
class UserInfo
{
public string name { get; set; }
public int age { get; set; }
public bool male { get; set; }
}
}
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class UserInfo
{
// Метод, реализующий словарь
public static Dictionary<int, string> MyDic(int i)
{
Dictionary<int, string> dic = new Dictionary<int,string>();
Console.WriteLine("Введите имя сотрудника: \n");
string s;
for (int j = 0; j < i; j++)
{
Console.Write("Name{0} --> ",j);
s = Console.ReadLine();
dic.Add(j, s);
Console.Clear();
}
return dic;
}
}
class Program
{
static void Main()
{
Console.Write("Сколько сотрудников добавить? ");
try
{
int i = int.Parse(Console.ReadLine());
Dictionary<int, string> dic = UserInfo.MyDic(i);
// Получить коллекцию ключей
ICollection<int> keys = dic.Keys;
Console.WriteLine("База данных содержит: ");
foreach (int j in keys)
Console.WriteLine("ID -> {0} Name -> {1}",j,dic[j]);
}
catch (FormatException)
{
Console.WriteLine("Неверный ввод");
}
Console.ReadLine();
}
}
}
var instance = new {Name = "Alex", Age = 27}
class Anonymous0001 // ссылочный тип
{
public string Name { get; private set; } // из других классов выглядит как read-only свойство
public int Age { get; private set; }
public Anonymous0001(string name, int age)
{
Name = name;
Age = age;
}
}
class Program
{
static void Main()
{
var instance = new Anonymous0001("Alex", 27);
}
}
var a = new System.Collections.Generic.Dictionary<string, int>() { { "vasya", 0 }, { "kolya", 0 }, { "alex", 1} };
int i = 0;
var result = from item in a where item.Value == 0
select new { Index = i++, Name = item.Key, Id = item.Value }; // создание объекта анонимного типа
foreach(var res in result) {
bool first = true;
foreach (var prop in res.GetType().GetProperties()) {
if (first) first = false;
else Console.Write(", ");
Console.Write("{0} = {1}", prop.Name, prop.GetValue(res, null));
}
Console.WriteLine();
}
Пользователи часто забывают свои пароли и напрягают первую линию тех. поддержки
private Button FirstButton = null;
void S_MouseClick(object sender, MouseEventArgs e)
{
var button = (sender as Button);
if (this.FirstButton == null)
{
// это первая кнопка в текущей сессии,
// запоминаем ссылку на кнопку
this.FirstButton = button;
}
else
{
// это вторая кнопка в текущей сессии
// сравниваем текст с первой
if (this.FirstButton.Text == button.Text)
{
Console.WriteLine("Текст совпадает!");
// меняем свойства кнопок
this.FirstButton.Text = button.Text = "--";
this.FirstButton.Enabled = button.Enabled = false;
}
else
{
Console.WriteLine("Текст не совпадает.");
}
// сбрасываем выбор, запуская тем самым новую сессию
this.FirstButton = null;
}
}