Tuesday, March 10, 2015

C# list AddRange Method & C# program that uses AddRange

AddRange adds an entire collection of elements. It can replace tedious foreach-loops that repeatedly call Add on List. We can pass any IEnumerable collection to AddRange, not just an array or another List.

The term "range" simply means an IEnumerable collection.


using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
 List<int> a = new List<int>();
 a.Add(1);
 a.Add(2);
 a.Add(5);
 a.Add(6);

 // Contains:
 // 1
 // 2
 // 5
 // 6

 int[] b = new int[3];
 b[0] = 7;
 b[1] = 6;
 b[2] = 7;

 a.AddRange(b);

 // Contains:
 // 1
 // 2
 // 5
 // 6
 // 7 [added]
 // 6 [added]
 // 7 [added]
 foreach (int i in a)
 {
     Console.WriteLine(i);
 }
    }
}

Output

1
2
5
6
7
6
7




C# program that adds objects to List

using System.Collections.Generic;

class Program
{
    static void Main()
    {
 // Add three objects to a List.
 List<Test> list = new List<Test>();
 list.Add(new Test(1, 2));
 list.Add(new Test(3, 4));
 list.Add(new Test(5, 6));
    }

    class Test
    {
 int _a;
 int _b;
 public Test(int a, int b)
 {
     _a = a;
     _b = b;
 }
    };
}

C# List Add Method

Add places an element at the end of a List. It handles both value types and reference types. The Add instance method on the List type has internal logic that allows you to quickly add elements to the end of the collection.


using System.Collections.Generic;

class Program
{
    static void Main()
    {
 // Add first four numbers to the List.
 List<int> primes = new List<int>();
 primes.Add(2);
 primes.Add(3);
 primes.Add(5);
 primes.Add(7);
    }
}

C# program that adds elements to List

using System.Collections.Generic;

class Program
{
    static void Main()
    {
 List<int> list = new List<int>();
 list.Add(2);
 list.Add(3);
 list.Add(5);
 list.Add(7);
    }
}

C# List

List. An array does not dynamically resize. A List does. With it, we do not need to manage the size on our own. This type is ideal for linear collections not accessed by keys

Dynamic in size, with many methods, List makes life easier. List is a generic (constructed) type. We need to use < and > in the List declaration. Lists handle any element type.

Wednesday, February 25, 2015

AngularJs append string by comma

  var roomUtiliesstr = '';
        angular.forEach($scope.RoomUtilisitesCollection, function (item) {

            if (item) {
                if (item.value == 'true') {
                    roomUtiliesstr += item.Title + ',';
                    // selectedUtility.push(item.Title);
                    //$scope.view.RoomUtilities.push(item.Title);
                }
            }
        });

Tuesday, February 24, 2015

AngularJs $http

The $http service is a core Angular service that facilitates communication with the remote HTTP servers via the browser's XMLHttpRequest object or via JSONP.

The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise with two $http specific methods: success and error.

// Simple GET request example :
$http.get('/someUrl').
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });
// Simple POST request example (passing data) :
$http.post('/someUrl', {msg:'hello word!'}).
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });