c# - a programmer's dream come true

Post on 17-Jul-2015

138 Views

Category:

Software

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

C#

A Programmer‘s Dream Come True

Alexander Pacha

HEY, I JUST MET YOUStoring basic data

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; }

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

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

HERE‘S MY NUMBERCALL ME MAYBE

Parameters and Delegates

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;

}

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);

THIS IS CRAZY

Lambdas and Functional Programmings Aspects

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;

}

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

Extension Methods

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)

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

Exception Handling in Tasks

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);

}); }

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)

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());

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#

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

top related