c# - a programmer's dream come true

18
C# A Programmer‘s Dream Come True Alexander Pacha

Upload: alexander-pacha

Post on 17-Jul-2015

138 views

Category:

Software


2 download

TRANSCRIPT

Page 1: C# - A Programmer's Dream Come True

C#

A Programmer‘s Dream Come True

Alexander Pacha

Page 2: C# - A Programmer's Dream Come True

HEY, I JUST MET YOUStoring basic data

Page 3: C# - A Programmer's Dream Come True

Classes, Auto-Properties, Enums

public class Contact

{

public string Name { get; set; }

public string PhoneNumber { get; set; }

public int BirthYear { get; set; }

public Gender Sex { get; set; }

}

public enum Gender

{

Male,

Female

}

private set; }

Page 4: C# - A Programmer's Dream Come True

Collections, Object Initializer

Contact hotGirl = new Contact();hotGirl.Name = "Carla Rae Jepsen";hotGirl.PhoneNumber = "555-10522";hotGirl.BirthYear = 1985;hotGirl.Sex = Gender.Female;

Contact thatDude = new Contact(){

Name = "Hot dude",PhoneNumber = "555-11923",

};

List<Contact> AddressBook = new List<Contact> {

hotGirl, thatDude

};

Object Initializer

Collection Initializer

Page 5: C# - A Programmer's Dream Come True

Foreach-Loops

foreach (var contact in AddressBook)

{

Console.WriteLine(contact);

}

public class Contact

{

...

public override string ToString()

{

return string.Format("Name: {0}, PhoneNumber: {1}, BirthYear: {2}, Sex: {3}", Name, PhoneNumber, BirthYear, Sex);

}

}

Foreach loop + implicit type declaration

Page 6: C# - A Programmer's Dream Come True

HERE‘S MY NUMBERCALL ME MAYBE

Parameters and Delegates

Page 7: C# - A Programmer's Dream Come True

Input and Output Parameters

public string WhatIsYourName(string yourName, string greeting)

{

return String.Format("{0} {1}. I'm {2}", greeting, yourName, Name);

}

hotGirl.WhatIsYourName("Alex", "Hello");

hotGirl.WhatIsYourName(greeting: "Hello", yourName: "Alex"));

public void GimmeYourPhoneNumberAndName(out string phoneNumber, out string name)

{

phoneNumber = "won't tell you";

name = Name;

}

Page 8: C# - A Programmer's Dream Come True

Delegates and Actions

public void CallMeMaybe(Action<string> callback)

{

if (Sex == Gender.Female)

callback(PhoneNumber);

}

private void OutputPhoneNumber(string herPhoneNumber)

{

Console.WriteLine("I got her number. It is: {0}", herPhoneNumber);

}

hotGirl.CallMeMaybe(OutputPhoneNumber);

Page 9: C# - A Programmer's Dream Come True

THIS IS CRAZY

Lambdas and Functional Programmings Aspects

Page 10: C# - A Programmer's Dream Come True

Lambda ExpressionsAnonymous function of the form

(input1, input2, …) => {method body}

E.g.

(Contact contact) => contact.BirthYear

In use:

int sum = AddressBook.Sum(c => c.BirthYear);

List<Contact> adults =

AddressBook.FindAll(contact =>

contact.BirthYear < 1996 &&

contact.BirthYear > 1900);

private int GetBirthYear(Contact contact) {

return contact.BirthYear;

}

Page 11: C# - A Programmer's Dream Come True

BEFORE YOU CAME INTO MY LIFE, I MISSED YOU SO BAD

Extension Methods

Page 12: C# - A Programmer's Dream Come True

Extension Methods

public static class ContactExtension

{

public static int WhatsYourAge(

{

return DateTime.Now.Year - contact.BirthYear;

}

}

Console.WriteLine(hotGirl.WhatsYourAge());

var adults = AddressBook.Where(contact => contact.WhatsYourAge() > 18);

this Contact contact)Contact contact)

Page 13: C# - A Programmer's Dream Come True

YOU TOOK YOUR TIME WITH THE CALL, I TOOK NO TIME WITH THE FALL

Exception Handling in Tasks

Page 14: C# - A Programmer's Dream Come True

Tasks and Exceptions

public void CallMeMaybe(Action<string> callback){

Task callHimBackTask = Task.Run(() =>{

Thread.Sleep(1000); // Sleep for the night

bool doesHeExist = callback != null;if (doesHeExist)

callback(PhoneNumber); else

throw new NullReferenceException("Invalid callback"); });

callHimBackTask.ContinueWith(task =>{

if(task.IsFaulted)Console.WriteLine(task.Exception);

}); }

Page 15: C# - A Programmer's Dream Come True

C++ and C#

• Goal: Making interop and use of unmanagedtypes easy

• C++ / CLI (Common Language Infrastructure)– Introduced in 2005– Runs on Common Language Runtime– Generated managed code

• C++ / CX (Component Extensions)– Introduced in 2011– Runs on Windows Runtime (WinRT)– Generates native code (x64, x86, ARM)

Page 16: C# - A Programmer's Dream Come True

C++ / CLI Example

public ref class TestClass{public:String^ WhatIsYourName();

};

String^ TestClass::WhatIsYourName(){

return gcnew String("My name is Alex");

}

TestClass clrTest = new TestClass();string name = clrTest.WhatIsYourName();

TestClass.h

TestClass.cpp

Usage in C#

std::string myString = "My name is Alex";return gcnew String(myString.c_str());

Page 17: C# - A Programmer's Dream Come True

C++ / CX Example

public ref class TestClass sealed{public:String^ WhatIsYourName();

};

String^ TestClass::WhatIsYourName(){

return ref new String(L"My name is Alex");

}

TestClass clrTest = new TestClass();string name = clrTest.WhatIsYourName();

TestClass.h

TestClass.cpp

Usage in C#

Page 18: C# - A Programmer's Dream Come True

Summary

• C# is mature and expressive

• Huge standard library and tool support

• Programmer friendly syntaxint numberOfContacts = AddressBook.Count;

var fib = new[] {1, 1, 2, 3, 5};

• Requires .NET-Runtime

• Constantly evolving