Visualizzazione post con etichetta english. Mostra tutti i post
Visualizzazione post con etichetta english. Mostra tutti i post

lunedì 4 luglio 2016

C# for n00bs, part one

Introduction to C# for N00bs who wants to start coding

It's a long time since the introduction of C# (read C Sharp) language at the beginning of the 00s. Sixteen years have passed since the introduction of .NET and C#, and the language is still in its evolution cycle. The last release, C# 6.0, was introduced just eleven months ago. 

C# is multi paradigm language as:
  • it's imperative
  • it's generic
  • it's object oriented
  • can be used for function programming
  • it's component oriented
also
  • it's strongly typed
  • it's declarative
Nearly all of these characteristics will be seen later in the article. C# is also an ISO Standard (ISO/IEC 23270:2006) and an ECMA Standard (ECMA-334), so everyone can develop its personal implementation.

It's all about data and instructions to use it

Nearly every programming language is based on two fundamental concepts: data and instructions. The first it's about "what" we're working with. The second one it's about "how and when" we're working with.

Just imagine a flow like this:
  1. Take a pot.
  2. Fill it with 250 ml of water.
  3. Place the pot on fire.
A developer will see the pot and the water as the data part of the flow, and the list above as the instructions. It's easy uh?

When developing a software, you need to "declare", aka tell the machine which data types to use and how to call them. You know, if you have one pot, it's just "pot". When you have seven pot, you must call them "Doc", "Grumpy", and so on because you need and unique identifier for each pot. Once "declared", data con be used in instructions. So you can "fill Grumpy with 250 ml of Water". Simply enough, uh?

Giving a first definition of "object" let's say it's the virtual correspondence to a real life object, like a pot, or a more abstract concept, like "communication" or "signal". An object is the sum of data and instruction used to define, or reproduce, the wanted behaviour of an element.

Introduction to data types

You've expected this. "Can I use only pots in my software?". Of course not. You have a set of basic types, the so called "simple" types, that are used to build more complex data. And you have objects, the complex data itself. 

Simple types

byte: the smallest numeric type. It's just, well, one byte, or 8 bits if you want. Can represent values just between 0 and 255. It's not signed, and it is one of the base types used in computer sciences. When you talk about data, you talk about one or aggregation of bytes.

int: it's a numeric type, represents a number between -2,147,483,648 and 2,147,483,647, with sign of course. It's a 32 bit numeric type. For now, just take it as a dogma.

long: it's another numeric type. It's just like int, but represents a number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. Long it's also signed as you can see. It's a 64 bit numeric type.

float: the first numeric type that supports fractional parts. It's used to say "floating point types", with the point used as usual for separation between decimal and fractional part. The point it's "floating" because the position is not fixed on the number. Example: 2.34 or 23.4. The point "floats" in memory. Float can represent numbers between ±1.5^−45 and ±3.4^38, with a precision of 7 digits. That's a lot. Float it's a 32 bit signed floating point number.

double: double it's just like float, but can represent numbers between ±5.0 × 10^−324 and ±1.7 × 10^308, with a precision of 16 digits. Double it's huge. It's a 64 bit signed floating point number.

string: friend of so much devs, a string represent the dev concept of "text". Strings store textual data in memory and allow text manipulation. It differs from other simple types for great degree, as it's also and object. But for now just take it, a string is a text bunch of data.

Complex data

You can combine the simple data types above to construct much more complex custom data types. These are structures, classes, enumerations and interfaces. We cannot talk now about them, as we must first cover other aspects of the language.

Special mention: namespaces

C# empathizes separation of concerns parading, which means that your program will be separated in small pieces of code built exactly to address separate concerns. This separation it's done not only at object level, it's obvious that a pot cannot be a car. It's also done using namespaces. Think at them like drawers. I will put in "Network" namespace all my objects related to network communications, and "Animations" everything related to animations in my graphic interface. So a namespace is a container used to organize and limit access to objects in your application.

It's all for now. Next time we will start coding the first very basic stuff. We will start talking about the basic structure of a program then start with some basic computations and data access. Enjoy!

domenica 15 giugno 2014

About the Windows Phone 8.1 hardware buttons

Windows Phone 8.1 is a huge step forward compared to the past towards a real unification of the source code between the desktop and mobile platform. Unifying the controls and start using programming patterns as MVVM to uncouple the visual component from the model logic it is now possible to write applications which is in common with desktop and mobile scenarios and where only the pages have specific behaviors for the platform on which are running or, through Visual States, apps can even have common views for each platform and component placement will only vary depending on the form factor. Then, of course, a control for Windows Phone will be rendered and operated differently from the OS than the same control for Windows 8.1, though in principle the developer should matter little if compared to the form factor. The input system is entirely handled by the OS.

With Windows Phone 8.1 the lifecycle of the application has been changed in favour of the same Windows 8.1 Desktop model. In a Win81 project that uses the new WinPRT (Windows Phone RT), the hardware back button is handled by the OS as this means that common behavior is the exit from the application. This behavior is profoundly different than Windows Phone 7.1/8 and could create problems in the conversion of some applications. The solution to our compatibility problem is, however, very simple.

In a WinPRT project hardware buttons are handled by the HardwareButtons class that exposes a set of events related to back, volume and camera buttons. Through this class, we can write a control dedicated to the management of the "back" button according to the old behavior that we find in the Windows Phone 7.1 and 8 first generation:

  public class PhoneButtonsAwarePage
        : Page
    {
        #region .ctor

        public PhoneButtonsAwarePage()
            : base()
        {
            this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Required;
            HardwareButtons.BackPressed += HardwareButtons_BackPressed;
            HardwareButtons.CameraPressed += HardwareButtons_CameraPressed;
        }

        

        ~PhoneButtonsAwarePage()
        {
            try
            {
                HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
                HardwareButtons.CameraPressed -= HardwareButtons_CameraPressed;
            }
            catch(Exception)
            {

            }
        }

        #endregion

        #region Methods

        protected virtual void OnBackPressed(BackPressedEventArgs e)
        {
            if (this.Frame.CanGoBack)
            {
                e.Handled = true;
                this.Frame.GoBack();
            }
            else
                e.Handled = false;
        }

        protected virtual void OnCameraPressed(CameraEventArgs e)
        {            
        }

        #endregion

        #region Event Handlers

        private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
            this.OnBackPressed(e);
        }

        private void HardwareButtons_CameraPressed(object sender, CameraEventArgs e)
        {
            this.OnCameraPressed(e);
        }        

        #endregion
    }

Then just swap the standard page with this new class in XAML and you're done.

<common:PhoneButtonsAwarePage x:Class="Test.Views.TestPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:Test.Views"
      xmlns:common="using:Test.Common"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      RequestedTheme="Light"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>

    </Grid>
</common:PhoneButtonsAwarePage>

Our application will follow the rule: "as long as there are items in the backstack, go back, otherwise exit." Obviously the management method of the buttons is written as virtual, so you can handle the override for any special requirements of your app.

domenica 16 settembre 2012

Expose the Domain Service to other types of clients, formerly "SOAP Endpoint"

Don't believe who say that it's easy to expose a Domain Services using SOAP endpoints. Expose a RIA Domain Service to other clients, glad to the very infrastructure of RIA Services, it's not painless. First of all, it needs some changes to the web.config, that's not a problem at all.
You will get the very painful steps just when try to connect to your domain services. The RIA endpoints are automatically generated by the service itself, so, you have no way to control them (some workarounds exists but I feel like they can create problems in future releases of RIA Services, or even in different configurations, because they're hack on IIS URL registration).

domenica 22 luglio 2012

Lambda Expressions in .NET

Lambda expressions are anonymous functions used to create delegates and complex expressions. They can contains expressions-only instructions and complete functions bodies.
In the beginning I must admit that I haven't seen any necessity to use them inside a context like the .NET Framework. I've mostly seen them useful for interpreted languages like Javascript. Using LINQ, I always prefered to use SQL-like sintax against extension methods and lamdba expressions.  Instead, during a recent .NET Campus event, I've completely changed my mind. I have been illuminated when I saw lambda expressions applied to several sorting and selecting algorithms in XNA development. They're the best choice for who need to write fast delegates without getting dirty the class code with functions that will be used very rarely.