Search This Blog

Monday, January 19, 2015

NetworkChange Class C#

C# > System.Net > NetworkInformation > NetworkChange

Use this class to receive notification when the IP address of a network interface changes.
Changes can appears when:

  • disconnect network cable
  • out of range of a wireless Local Area Network
  • hardware failure
Events:





    C# NetworkInformation NameSpace

    C# > System.Net > NetworkInformation


    Provides access to network address information and traffic data.

    Classes





    Thursday, January 15, 2015

    ReadAllBytes C# Example

    C# > Files > File Class > ReadAllBytes

    Opens, reads the contents of the file into a byte array, and then closes the file.

    Example

    Read bytes from jpg file and display to PictureBox.

    var bytes = File.ReadAllBytes("D:\\Pics\\1.jpg");
    pictureBox1.Image = Image.FromStream(new System.IO.MemoryStream(bytes));







    C# File Class

    C# > Files > File Class


    Use the File class for:
    • copy, move, rename, create, open, delete to a single file at a time. 
    • get and set file attributes.


    All File methods are static.


    Methods





    Tuesday, January 6, 2015

    IsNumeric equivalent in LINQ example

    C# > LINQ > IsNumeric

    IsNumeric equivalent in LINQ example.

    double value;
    var total = (from DataRow dr in dataTable.Rows
                    where double.TryParse(dr["value"], out value)
                    select Convert.ToDouble(dr["value"])).Sum();





    Wednesday, December 24, 2014

    Dependency Injection C# Example

    C# > PatternsDependency Injection 

    Dependency injection promotes loose coupling of components by passing dependencies to an object rather than having an object instantiate its own dependencies.(http://www.blackwasp.co.uk/DependencyInjection.aspx)

           




    Example

            public interface ITruck
            {
                void Go(double distance);
            }


            public class Truck : ITruck
            {
                public double Consumption{ get; set; }
                public void Go(double distance)
                {
                    double necessary_fuel = Consumption * distance;
                }
            }


            public interface IRoute
            {
                void AddTruck(ITruck dependency);
            }


            public class Route : IRoute
            {
                ITruck _truck;
                public double Distance { get; set; }

                public void AddTruck(ITruck truck)
                {
                    _truck = truck;
                }

                public void TruckGo()
                {
                    _truck.Go(Distance);
                }
            }


                Route route = new Route();
                route.Distance = 100;

                Truck truck = new Truck();
                truck.Consumption = 20;
               
                route.AddTruck(truck);

                route.TruckGo();





    C# System.Security AccessControl

    C# > System.Security > AccessControl

    With AccessControl you can control, access and audit security related actions on securable objects.