Search This Blog

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.









    C# > System.Security

    C# > System.Security

    • NET Framework security system
    • provide access to authentication
    • provide crytographic services
    • control access to operations based on policy
    • rights management of application-created content
    Classes





      Friday, December 19, 2014

      Dynamic Type C# Example

      C# > Types > dynamic

      The dynamic enables bypass compile-time type checking. These operations are resolved at run time. 
      Example

      dynamic dyn = 1;
      object obj = 1;

      var dyn_type = dyn.GetType(); // System.Int32
      var obj_type = obj.GetType(); // System.Int32

      dyn = dyn + 1; //Ok
      obj = obj + 1; // Error    1      Operator '+' cannot be applied to operands of type 'object' and 'int'








      Object Type C# Example

      C# > Types > object

       You can assign values of any type to variables of type object

      Example

      object obj;
      obj = 1; 
      string type =  obj.GetType().Name; //Int32
         
      obj = "string";
      type = obj.GetType().Name; //String








      Thursday, December 18, 2014

      Dns Get Host By Name C# Example

      C# > System.Net > Dns

      Retrieves information about a specific host from the Internet Domain Name System (DNS).

      Example

        IPHostEntry hostInfo = Dns.GetHostByName("www.microsoft.com");
        string hostName = hostInfo.HostName;
        IPAddress[] addressList = hostInfo.AddressList;
        foreach (IPAddress ipAddress in addressList)
        {
                    

        }










      Monday, December 15, 2014

      C# System.Net

      C#System.Net

      Provides  programming interface for:

      • network protocols
      • cache policies
      • e-mail
      • network traffic data and network address information
      • networking functionality
      Classes:
      Namespaces