Friday, November 6, 2015

Data Type In c#

Data Types

Data Types define the type of data that a variable can store. Some variables store numbers, others store names. The built-in VB.NET type aliases and their equivalent .NET Framework types follow:

Integers

VB Alias

.NET Type

Size

Range

SByte

System.SByte

8 bits (1 byte)

-128 to 127

Byte

System.Byte

8 bits (1 byte)

0 to 255

Short

System.Int16

16 bits (2 bytes)

-32,768 to 32,767

UShort

System.UInt16

16 bits (2 bytes)

0 to 65,535

Integer

System.Int32

32 bits (4 bytes)

-2,147,483,648 to 2,147,483,647

UInteger

System.UInt32

32 bits (4 bytes)

0 to 4,294,967,295

Long

System.Int64

64 bits (8 bytes)

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

ULong

System.UInt64

64 bits (8 bytes)

0 to 18,446,744,073,709,551,615

Floating-point

VB Alias

.NET Type

Size

Precision

Range

Single

System.Single

32 bits (4 bytes)

7 digits

1.5 x 10-45 to 3.4 x 1038

Double

System.Double

64 bits (8 bytes)

15-16 digits

5.0 x 10-324 to 1.7 x 10308

Decimal

System.Decimal

128 bits (16 bytes)

28-29 decimal places

1.0 x 10-28 to 7.9 x 1028

Other pre-defined types

VB Alias

.NET Type

Size (bits)

Range

Char

System.Char

16 bits (2 bytes)

One Unicode symbol in the range of 0 to 65,535.

Boolean

System.Boolean

32 bits (4 bytes)

True or False

Object

System.Object

32/64 bits (4/8 bytes)

Platform dependent (a reference to an object).

Date

System.DateTime

64 bits (8 bytes)

January 1, 0001 12:00:00 AM to December 31, 9999 11:59:59 PM

String

System.String

80 + [16 * Length] bits (10 + [2 * Length] bytes)

A Unicode string with a maximum length of 2,147,483,647 characters.

 

Value Types

·         All numeric data types

·         Boolean, Char, and Date

·         All structures, even if their members are reference types

·         Enumerations, since their underlying type is always SByte, Short, Integer, Long, Byte, UShort, UInteger, orULong

Reference Types

·         String

·         All arrays, even if their elements are value types

·         Class types, such as Form

·         Delegates

Elements That Are Not Types

·         Namespaces

·         Modules

·         Events

·         Properties and procedures

·         Variables, constants, and fields

 

Collection - Still working on... ! :)

Collections

Dynamic Array

Fast lookup

Required Cast

Good Choice for Booleans

Thread safety

Stores Any type object

Linear Array

Loop

Comments

List

Yes

Not good

No

No

no

No(Except List<object> )

Yes

It is fast because it is Linear

 

Dictionary

Yes

Yes

No

No

No

 

 

 

 

ArrayList

yes

No

Yes

No

No

Yes

Yes

 

 

Hashtable

 

Yes

 

 

 

 

 

 

It is an older .NET Framework type.

ConcurrentDictionary 

Yes

Yes

No

No

Yes

No

 

 

 

ConcurrentBag

 

 

 

 

 

 

 

 

Sometimes lookup functionality is not required. ConcurrentBag allows us to safely Add and Take results

BitArray

 

 

 

yes

 

 

 

 

The program creates a bool array with true and false values, and then the BitArray constructor converts those into one bit each. This means that instead of one byte for a bool, the values are stored as one bit, in one-eighth the space.

Tuple

 

 

 

 

 

 

 

 

A Tuple has many items. Each item can have any type. The Tuple class provides a unified syntax for creating objects with typed fields.

KeyValuePair

 

 

 

 

 

 

 

 

Tuple was faster in every test than KeyValuePair except in allocation performance.

Stack

 

 

 

 

 

 

 

 

 

Queue

 

 

 

 

 

 

 

 

 

HashSet

 

 

 

 

 

 

 

 

HashSet is an optimized set collection. It helps eliminates duplicate strings or elements in an array. It provides a simple syntax for taking the union of elements in a set. This is performed in its constructor.

Constructor

 

Example. This program contains a source array that contains several duplicated strings. It eliminates duplicate strings in the array. The program calls the HashSet constructor to transform the array elements into a set data structure.

 

When the source input becomes large with thousands of elements, hashed collections are faster.

 

SortedSet

 

 

 

 

 

 

 

 

SortedSet is an ordered set collection. We have many elements and want to store them in a sorted order and also eliminate all duplicates from the data structure. The SortedSet is part of the System.Collections.Generic namespace.

 

 

 

 

 

 

 

 

 

Lazy

 

 

 

 

 

 

 

 

This type implements the lazy instantiation pattern. We look into the concept of thunks, as described in a programming textbook. The Lazy class has no relation to the Sleep method.

HybridDictionary

 

 

 

 

 

 

 

 

HybridDictionary attempts to optimize Hashtable. It implements a linked list and hash table data structure, switching over to the second from the first when the number of elements increases past a certain threshold.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                                                                                                                                                                                                                                        

Search a string in SQL Stored Procedure

DECLARE @Search varchar(255)

SET @Search='COMPLETE'

 

SELECT DISTINCT

    o.name AS Object_Name,o.type_desc,m.definition

    FROM sys.sql_modules        m

        INNER JOIN sys.objects  o ON m.object_id=o.object_id

    WHERE m.definition Like '%'+@Search+'%'

    ORDER BY 2,1

Genarate Links on View in MVC

It's always  better to  generate a URL using “Url. Action” method to avoid URL issues. When Deploying application from one environment to other environments, it may not necessary to have a same website configuration on IIS. Means, On development you may have a virtual directory structure is “www.test.com/application/results” but in other environments, it configured as “www.test.com/websitename/application/results” so we need to consider all these Directory structures when we are generating URL otherwise we run into issues. URL. Action method will take care of these things so it is a good idea to  use URL. Action method to avoid URL issues   

  

Example: Url.Action("results", "Application", new { id = Model.id })                                                                                         

 

 

 

 

 

 

 

 

 

 

 

                                                                                                                                                                      

Tuesday, July 14, 2015

Custome validations in MVC

Validations are very important in any application  and Validations restrict the invalid data being entered into the application.

In MVC we can build a custom validations framework by extending “ValidationAttribute”, “IClientValidatable”.

 

The “ValidationAttribute“ class serves as base class for all  validations  and  By overriding “Isvalid” method we can  extend the validations.

The ValidateAttribute take two parameters value and the Validation context which holds all the control values.

 

For example I want to compare   two dates.

 

Create a class with extends the validation attribute and create a property of compare to date field name. using validation Context, retrieve the value the compare to date field value and then

Compare with Self value .

 public class GreaterDateAttribute : ValidationAttribute

{

 

  public string EarlierDateField { get; set; }

 

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)

        {

            DateTime? date = value != null ? (DateTime?)value : null;

            var earlierDateValue = validationContext.ObjectType.GetProperty(EarlierDateField)

                .GetValue(validationContext.ObjectInstance, null);

            DateTime? earlierDate = earlierDateValue != null ? (DateTime?)earlierDateValue : null;

 

            if (date.HasValue && earlierDate.HasValue && date < earlierDate)

            {

                return new ValidationResult(ErrorMessage);

            }

 

            return ValidationResult.Success;

        }

}