Search This Blog

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










STR SQL Server Example

SQL Server > Built-in Functions > STR

Converts number to varchar.


STR ( expression , length , decimal )

Example:

SELECT STR(123.45, 6, 1);

GO

result
 123.5





Assembly Class C# Example

C# > System > Reflection > Assembly

Assembly class:

  • load assemblies
  • explore the metadata 
  • find the types contained in assemblies and to create instances of those types.




Example


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication27
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Assembly a = typeof(Form1).Assembly;
            MessageBox.Show(a.FullName);
        }
    }
}





Reflection C#

C# > System > Reflection

Reflection provides objects that encapsulate assemblies, modules and types.
With reflection you can read metadata for finding assemblies, modules and types information at runtime. 
These objects also can be used to manipulate instances of loaded types.