int a = 20;
int b = a;
a = 40;
Console.WriteLine(b);
static void Main(string[] args)
{
Cat c1 = new Cat();
c1.Name = "Billy";
Cat c2 = c1;
c2.Name = "Hanna";
Console.WriteLine(c1.Name);
//Hanna
}
public class Cat
{
public string Name { get; set; }
}
int i = 123;
// The following line boxes i.
object o = i;
o = 123;
i = (int)o; // unboxing
private static string result;
static void Main()
{
SaySomething();
Console.WriteLine(result);
}
static async Task<int> SaySomething()
{
await Task.Delay(10);
result = "Hello!";
return 1;
}
private static string result;
static void Main()
{
SaySomething();
Console.WriteLine(result);
}
static async Task<int> SaySomething()
{
await Task.Delay(10);
result = "Hello!";
return 1;
}
private static string result;
static void Main()
{
SaySomething();
Console.WriteLine(result);
}
static async Task<int> SaySomething()
{
Thread.Sleep(10);
result = "Hello!";
return 1;
}
private static string result;
static void Main()
{
SaySomething();
Console.WriteLine(result);
}
static async Task<int> SaySomething()
{
Thread.Sleep(10);
result = "Hello!";
return 1;
}
List<Task> tasks = new List<Task>();
for (int i = 0; i < 80; i++)
{
tasks.Add(new Task(() => Console.WriteLine(i)));
}
foreach (var item in tasks)
{
item.RunSynchronously();
}
List<Task> tasks = new List<Task>();
for (int i = 0; i < 80; i++)
{
tasks.Add(new Task(() => Console.WriteLine(i)));
}
foreach (var item in tasks)
{
item.RunSynchronously();
}
for (int i = 0; i < 80; i++)
{
Task.Run(() => Console.WriteLine(i));
}
for (int i = 0; i < 11; i++)
{
Task.Run(() => Console.WriteLine(i));
}
Console.Read();
for (int i = 0; i < 80; i++)
{
Task.Run(() => Console.WriteLine(i));
}
for (int i = 0; i < 11; i++)
{
Task.Run(() => Console.WriteLine(i));
}
Console.Read();
List<Task> tasks = new List<Task>();
for (int i = 0; i < 80; i++)
{
int newi = i;
tasks.Add(new Task(() => Console.WriteLine(newi)));
}
foreach (var item in tasks)
{
item.RunSynchronously();
}
List<Task> tasks = new List<Task>();
for (int i = 0; i < 80; i++)
{
int newi = i;
tasks.Add(new Task(() => Console.WriteLine(newi)));
}
foreach (var item in tasks)
{
item.RunSynchronously();
}
for(var i=0; i<4; i++) {
setTimeout(
(function(x) {
return function() {
console.log("i=="+i+", x=="+x);
};
})(i)
, i*100);
}
for(var i=0; i<4; i++) {
setTimeout(
(function(x) {
return function() {
console.log("i=="+i+", x=="+x);
};
})(i)
, i*100);
}
public class Shape
{
}
public class Square : Shape
{
}
public class Triangle : Shape
{
}
public class Program
{
static void Main(string[] args)
{
var shapes = new Shape[]
{
new Square(),
new Triangle()
};
}
}
public class Shape
{
public virtual void Draw()
{
}
}
public class Square : Shape
{
public override void Draw()
{
Console.WriteLine("Draw Square");
}
}
public class Triangle : Shape
{
public override void Draw()
{
Console.WriteLine("Draw Triangle");
}
}
public class Program
{
static void Main(string[] args)
{
var shapes = new List<Shape>()
{
new Square(),
new Triangle()
};
foreach (var shape in shapes)
{
shape.Draw();
}
}
}
void Method(ref int refArgument)
{
refArgument = refArgument + 44;
}
int number = 1;
Method(ref number);
Console.WriteLine(number);
class RefOverloadExample
{
public void SampleMethod(int i) { }
public void SampleMethod(ref int i) { }
}
void Method(out int answer, out string message,
out string stillNull)
{
answer = 44;
message = "I've been returned";
stillNull = null;
}
int argNumber;
string argMessage, argDefault;
Method(out argNumber, out argMessage, out argDefault);
Console.WriteLine(argNumber);
Console.WriteLine(argMessage);
Console.WriteLine(argDefault == null);
public class Employee
{
public string name;
protected decimal basepay;
public Employee(string name, decimal basepay)
{
this.name = name;
this.basepay = basepay;
}
public virtual decimal CalculatePay()
{
return basepay;
}
}
public class SalesEmployee : Employee
{
private decimal salesbonus;
public SalesEmployee(string name, decimal basepay,
decimal salesbonus) : base(name, basepay)
{
this.salesbonus = salesbonus;
}
public override decimal CalculatePay()
{
return basepay + salesbonus;
}
}
abstract class Shape
{
public abstract int GetArea();
}
class Square : Shape
{
int side;
public Square(int n) => side = n;
// GetArea method is required to avoid a compile-time error.
public override int GetArea() => side * side;
static void Main()
{
var sq = new Square(12);
Console.WriteLine($"Area of the square = {sq.GetArea()}");
}
}
public sealed class Foo : IDisposable
{
private readonly IDisposable _bar;
public Foo()
{
_bar = new Bar();
}
public void Dispose()
{
_bar?.Dispose();
}
}
public void AddToFile(string text)
{
using (System.IO.FileStream file = new System.IO.FileStream("test.txt",
System.IO.FileMode.Append))
{
byte[] encodedText = System.Text.Encoding.Default.GetBytes(text);
file.Write(encodedText, 0, encodedText.Length);
}
}
public void AddToFile(string text)
{
{
System.IO.FileStream file = new System.IO.FileStream("test.txt",
System.IO.FileMode.Append);
try
{
byte[] encodedText = System.Text.Encoding.Default.GetBytes(text);
file.Write(encodedText, 0, encodedText.Length);
}
finally
{
if (file != null)
{
file.Dispose();
}
}
}
}
class Program
{
Program()
{
throw new Exception();
}
//A destructor in C# overrides System.Object.Finalize
//Manually overriding Finalize will give you an error message.
~Program()
{
Console.WriteLine("Finalizer is called");
}
static void Main()
{
try
{
new Program();
}
catch { }
}
}
public void GCTestFinalize()
{
StreamWriter stream = File.CreateText("a.txt");
stream.Write("TEST");
GC.Collect();
GC.WaitForPendingFinalizers();
File.Delete("a.txt");
}
public class UsingEnum where T : System.Enum { }
public class UsingDelegate where T : System.Delegate { }
public class Multicaster where T : System.MulticastDelegate { }
public class MyGenericClass where T : IComparable, new()
{
// The following line is not possible without new() constraint:
T item = new T();
}
using (var conn = new SqlConnection("connection string"))
{
conn.Open();
// Execute SQL statement here on the connection you created
}
{
using FileStream f = new FileStream(@"C:\using.md");
}
// Covariance Kowariancja
IEnumerable<string> strings = new List<string>();
IEnumerable<object> objects = strings;
// Contravariance. Kontrawariancja
// static void SetObject(object o) { }
Action<object> actObject = SetObject;
Action<string> actString = actObject;
static object GetObject() { return null; }
static void SetObject(object obj) { }
static string GetString() { return ""; }
static void SetString(string str) { }
static void Test()
{
// Covariance. A delegate specifies a return type as object,
// but you can assign a method that returns a string.
Func<object> del = GetString;
// Contravariance. A delegate specifies a parameter type as string,
// but you can assign a method that takes an object.
Action<string> del2 = SetObject;
}
class UnsafeTest
{
// Unsafe method: takes pointer to int.
unsafe static void SquarePtrParam(int* p)
{
*p *= *p;
}
unsafe static void Main()
{
int i = 5;
// Unsafe method: uses address-of operator (&).
SquarePtrParam(&i);
Console.WriteLine(i);
}
}
var list = new List();
// ┌┐
// ││ Count: 0
// └┘ Capacity: 0
// ┌───┬───┬───┬───┐
list.Add('h'); // │ h │ ░ │ ░ │ ░ │ Count: 1
// └───┴───┴───┴───┘ Capacity: 4
// ┌───┬───┬───┬───┐
list.Add('e'); // │ h │ e │ ░ │ ░ │ Count: 2
// └───┴───┴───┴───┘ Capacity: 4
// ┌───┬───┬───┬───┐
list.Add('l'); // │ h │ e │ l │ ░ │ Count: 3
// └───┴───┴───┴───┘ Capacity: 4
// ┌───┬───┬───┬───┐
list.Add('l'); // │ h │ e │ l │ l │ Count: 4
// └───┴───┴───┴───┘ Capacity: 4
// ┌───┬───┬───┬───┬───┬───┬───┬───┐
list.Add('o'); // │ h │ e │ l │ l │ o │ ░ │ ░ │ ░ │ Count: 5
// └───┴───┴───┴───┴───┴───┴───┴───┘ Capacity: 8
// ┌───┬───┬───┬───┬───┬───┬───┬───┐
list.Add(' '); // │ h │ e │ l │ l │ o │ │ ░ │ ░ │ Count: 6
// └───┴───┴───┴───┴───┴───┴───┴───┘ Capacity: 8
// ┌───┬───┬───┬───┬───┬───┬───┬───┐
list.Add('w'); // │ h │ e │ l │ l │ o │ │ w │ ░ │ Count: 7
// └───┴───┴───┴───┴───┴───┴───┴───┘ Capacity: 8
// ┌───┬───┬───┬───┬───┬───┬───┬───┐
list.Add('o'); // │ h │ e │ l │ l │ o │ │ w │ o │ Count: 8
// └───┴───┴───┴───┴───┴───┴───┴───┘ Capacity: 8
// ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
list.Add('r'); // │ h │ e │ l │ l │ o │ │ w │ o │ r │ ░ │ ░ │ ░ │ ░ │ ░ │ ░ │ ░ │ Count: 9
// └───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘ Capacity: 16
// ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
list.Add('l'); // │ h │ e │ l │ l │ o │ │ w │ o │ r │ ░ │ ░ │ ░ │ ░ │ ░ │ ░ │ ░ │ Count: 10
// └───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘ Capacity: 16
// ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
list.Add('d'); // │ h │ e │ l │ l │ o │ │ w │ o │ r │ l │ d │ ░ │ ░ │ ░ │ ░ │ ░ │ Count: 11
// └───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘ Capacity: 16
foreach(Department dept in context.Departments) {
Console.WriteLine(dept.Name);
foreach(Employee emp in dept.Employees) {
Console.WriteLine("\t" + emp.FirstName
+ " " + emp.LastName);
}
}
try
{
TrySomeCodeThatMightException();
}
finally
{
CleanupEvenOnFailure();
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("before");
Console.WriteLine(test());
Console.WriteLine("after");
}
static string test()
{
try
{
return "return";
}
finally
{
Console.WriteLine("finally");
}
}
}
static public void Main()
{
// Data source
int[] sequence = {2, 51, 52,
5, 45, 6};
var result = from s in sequence
let a1
= s + 100 where a1 > 150 select a1;
foreach(var val in result)
{
Console.WriteLine(val);
}
}
for (var i = 0; i < 10000; i++)
{
using (var httpClient = new HttpClient())
{
var result = await httpClient.GetAsync("http://cezarywalecniuk.pl");
result.EnsureSuccessStatusCode();
}
}
document.addEventListener('click',
() => console.log(this));
document.addEventListener('click',
() => console.log(this));
(function(a) {
return (function(b) {
console.log(a+b);
})(2);
})(1);
var myButton = {
content: 'OK',
click() {
console.log(this.content + ' clicked');
}
};
myButton.click();
var looseClick = myButton.click;
looseClick(); // not bound, 'this' is not myButton - it is the globalThis
var boundClick = myButton.click.bind(myButton);
boundClick(); // bound, 'this' is myButton
var func = function() {
console.log(this)
}.bind(1);
func();
function checkFun(a, b, c){
console.log(this);
console.log(a);
console.log(b);
console.log(c);
}
checkFun.call(1,2,3,4);
function checkFun(a, b, c){
console.log(this);
console.log(a);
console.log(b);
console.log(c);
}
checkFun.apply(1,[2,3,4]);
SELECT ShiftName,
Production,
Engineering,
Marketing
FROM (SELECT s.Name AS ShiftName,
h.BusinessEntityID,
d.Name AS DepartmentName
FROM HumanResources.EmployeeDepartmentHistory h
INNER JOIN HumanResources.Department d
ON h.DepartmentID = d.DepartmentID
INNER JOIN HumanResources.Shift s
ON h.ShiftID = s.ShiftID
WHERE EndDate IS NULL
AND d.Name IN ('Production', 'Engineering', 'Marketing')
) AS a
PIVOT
(
COUNT(BusinessEntityID)
FOR DepartmentName IN ([Production], [Engineering], [Marketing])
) AS b
ORDER BY ShiftName;
IF OBJECT_ID('tempdb.dbo.#Contact') IS NOT NULL DROP TABLE #Contact;
CREATE TABLE #Contact
(
EmployeeID INT NOT NULL,
PhoneNumber1 BIGINT,
PhoneNumber2 BIGINT,
PhoneNumber3 BIGINT
)
GO
INSERT #Contact
(EmployeeID, PhoneNumber1, PhoneNumber2, PhoneNumber3)
VALUES (1, 2718353881, 3385531980, 5324571342),
(2, 6007163571, 6875099415, 7756620787),
(3, 9439250939, NULL, NULL);
SELECT EmployeeID,
PhoneType,
PhoneValue
FROM #Contact c
UNPIVOT
(
PhoneValue
FOR PhoneType IN ([PhoneNumber1], [PhoneNumber2], [PhoneNumber3])
) AS p;
BEGIN TRANSACTION [Tran1]
BEGIN TRY
INSERT INTO [Test].[dbo].[T1] ([Title], [AVG])
VALUES ('Tidd130', 130), ('Tidd230', 230)
UPDATE [Test].[dbo].[T1]
SET [Title] = N'az2' ,[AVG] = 1
WHERE [dbo].[T1].[Title] = N'az'
COMMIT TRANSACTION [Tran1]
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION [Tran1]
END CATCH
SET TRANSACTION ISOLATION LEVEL
{ READ UNCOMMITTED
| READ COMMITTED
| REPEATABLE READ
| SNAPSHOT
| SERIALIZABLE
}
[Test]
public void Add_AddingTwoValues_ReturnsProperValue()
{
// Arrange:
var calc = new Calculator();
// Act:
int result = calc.Add(2, 3);
// Assert:
Assert.AreEqual(5, result);
}
static void Main(string[] args)
{
List<int> list = new List<int>()
{
1,3,5,7,11,12,24,36
};
var sum = Sum(list);
}
public static int Sum(List list)
{
}
static void Main(string[] args)
{
List<int> list = new List<int>()
{
1,3,5,7,11,12,24,36
};
var sum = Sum(list);
}
public static int Sum(List list)
{
int sum = 0;
for (int i = 0; i < list.Count; i++)
{
if (list[i] % 2 == 0)
sum += list[i];
}
return sum;
}
static void Main(string[] args)
{
string test = "Ala ma kota.";
var rev = ReverseWordsInSentence(test);
}
public static string ReverseWordsInSentence(string s)
{
}
static void Main(string[] args)
{
string test = "Ala ma kota.";
var rev = ReverseWordsInSentence(test);
}
public static string ReverseWordsInSentence(string s)
{
if (string.IsNullOrEmpty(s))
throw new ArgumentNullException("ReverseWordsInSentence(string s) have null parameter");
string[] arr = s.Split(" ");
StringBuilder sb = new StringBuilder();
for (int i = arr.Length - 1; i >= 0; i--)
{
string a = arr[i];
if (arr.Length - 1 == i)
{
a = a.Replace(".", "");
}
if (i == 0)
{
a = a + ".";
sb.Append(a);
}
else
{
sb.Append(a);
sb.Append(" ");
}
}
return sb.ToString();
}
static void Main(string[] args)
{
var array = new int[] { 1, 1, 1, 2, 3, 4, 5 };
var f = GetNotUniqueCount(array);
}
public static int GetNotUniqueCount(int[] array)
{
}
static void Main(string[] args)
{
var array = new int[] { 1, 1, 1, 2, 3, 4, 5 };
var f = GetNotUniqueCount(array);
}
public static int GetNotUniqueCount(int[] array)
{
if (array == null)
throw new ArgumentNullException("GetNotUniqueCount(int[] array) have null parameter array");
List<int> exists = new List<int>();
foreach (var item in array)
{
if (exists.Contains(item))
continue;
exists.Add(item);
}
return exists.Count;
}
static void Main(string[] args)
{
List<DateTime> daty = new List<DateTime>();
daty.Add(DateTime.Now.AddDays(-600));
daty.Add(DateTime.Now.AddDays(-400));
daty.Add(DateTime.Now.AddDays(-100));
daty.Add(DateTime.Now.AddDays(-50));
var f = Arrage(daty);
}
public static List<Tuple<DateTime, DateTime>>
Arrage(List<DateTime> array)
{
}
static void Main(string[] args)
{
List<DateTime> daty = new List<DateTime>();
daty.Add(DateTime.Now.AddDays(-600));
daty.Add(DateTime.Now.AddDays(-400));
daty.Add(DateTime.Now.AddDays(-100));
daty.Add(DateTime.Now.AddDays(-50));
var f = Arrage(daty);
}
public static List<Tuple<DateTime, DateTime>> Arrage(List<DateTime> array)
{
if (array == null)
throw new ArgumentNullException("Arrage(int[] array) have null parameter array");
List<Tuple<DateTime, DateTime>> tuples =
new List<Tuple<DateTime, DateTime>>();
int i = 1;
DateTime? d1 = null;
DateTime? d2 = null;
foreach (var item in array)
{
if (i % 2 == 0)
{
d2 = item;
tuples.Add(
new Tuple<DateTime, DateTime>(
d1.Value, d2.Value)
);
}
else
{
d1 = item;
}
i++;
}
return tuples;
}
static void Main(string[] args)
{
string[] correct = new string[] {"AB","ABAB" };
string[] notcorrect = new string[] { "AA", "BB","ABA","BA","ABB" };
}
public bool Check(string[] array)
{
}
static void Main(string[] args)
{
string[] correct = new string[] { "AB", "ABAB" };
string[] notcorrect = new string[] { "AA", "BB", "ABA", "BA", "ABB" };
var b1 = Check(correct);
var b2 = Check(notcorrect);
}
public static bool Check(string[] array)
{
for (int i = 0; i < array.Length; i++)
{
for (int k = 0; k < array[i].Length; k++)
{
if (k % 2 == 0)
{
if (array[i][k] != 'A')
return false;
}
else
{
if (array[i][k] != 'B')
return false;
}
}
}
return true;
}
static void Main(string[] args)
{
string[] correct = new string[] { "AB", "ABAB" };
string[] notcorrect = new string[] { "AA", "BB", "ABA", "BA", "ABB" };
var b1 = Check(correct);
var b2 = Check(notcorrect);
}
public static bool Check(string[] array)
{
for (int i = 0; i < array.Length; i++)
{
for (int k = 0; k < array[i].Length; k++)
{
var newstring = array[i].Replace("AB", "");
if (newstring.Length > 0)
return false;
}
}
return true;
}