What are generics in C#.NET?
Generic types to maximize code reuse, type safety, and performance. They can be used to create collection classes. Generic collection classes in the System.Collections.Generic namespace should be used instead of classes such as ArrayList in the System.Collections namespace.
or
Generics allow you to define type-safe data structures, without committing to actual data types. It allows writing less but more effective code. Generic provides flexibility and power but the developer needs to be responsible while using it.
A regular expression (RegExp) is an object that describes a pattern of characters. When you search in a text, you can use a pattern to describe what you are searching for. A simple pattern can be one single character. A more complicated pattern can consist of more characters, and can be used for parsing, format checking, substitution and more.
Regular expressions are used to perform powerful pattern-matching and search-and-replace functions on text. Syntax of RegExp is as follow :
var patterntxt=new RegExp(somepattern,modifiers);
or
var patt=/somepattern/modifiers;
In above code, somepattern specifies the pattern of an expression. modifiers specify if a search should be global, case-sensitive, etc.
RegExp Modifiers are used to perform case-insensitive and global searches. For example we use i modifier for case-insensitive matching and g modifier for global match. Global match means, find all matches rather than stopping after the first match. For example
var str1 = "This is some text.";
var pattern = /some text/i;
document.write(str1.match(pattern));
The result of above case-sensitive search will be : some text
The test() method searches a string for a specified value, and returns true or false, depending on the result. For example
var pattern=new RegExp("s");
document.write(pattern.test("The sun rises in the east"));
Result of above code will be "true", as it contains the "s" string.
The exec() method searches a string for a specified value, and returns the text of the found value. It return null, in case no match found. For example
var pattern=new RegExp("s");
document.write(pattern.exec("The sun rises in the east."));
Result of above code will be "s", as it contains the "s" string.
The Math object allows you to perform mathematical tasks. The Math object includes several mathematical constants and methods. Syntax for using properties/methods of Math is as follows:
var x=Math.PI;
var y=Math.sqrt(16);
There is eight mathematical constants in JavaScript that can be accessed from the Math object. These mathematical constants are :
E
PI
square root of 2
square root of 1/2
natural log of 2
natural log of 10
base-2 log of E
base-10 log of E.
We can call/reference these constants from JavaScript as below:
Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E
There are many mathematical methods also available in JavaScript. For example Math.round() is used to round a number, e.g
document.write(Math.round(9.8));
Result of above code will be : 10
Math.random() method of the Math object is used to generate random numbers. For example
document.write(Math.random());
Result of above code can be anything between 0 and 1.
We can set the limit of random number generation( by using arguments ). For example, the following code return random number between 0 and 20.
Math.random()*21)
To generate random number from 0 and 100 change 21 to 101.
The JavaScript Boolean object represents two values, "true" or/and "false". We can create a Boolean object in javascript as follows :
var myBooleanObj=new Boolean();
Javascript boolean object will set to false, if we do not set its initial value or has one of the following values:
0
-0
null
""
false
undefined
NaN
It will be true for any other value.
An array is a special variable, which can hold more than one value, at a time. For example, if you have a list of items, storing the country names in single variables could look like as follow :
var country1="Pakistan";
var country2="USA";
var country3="Russia";
An array can hold all your variable values under a single name. And you can access the values by referring to the array name. Each element in the array has its own ID so that it can be easily accessed. We can defined array in following ways :
var country=new Array();
country[0]="Pakistan";
country[1]="USA";
country[2]="Russia";
or
var country=new Array("Pakistan","USA","Russia");
or
var country=["Pakistan","USA","
Russia"];
You can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0. For example
document.write(country[1]);
Result of above code will be : USA
To modify a value in an existing array, just add a new value to the array with a specified index number:
country[1]="Germany";
This code change the value USA to Germany inside array object.
document.write(country[0]);
Now It will result as : Germany