30 June 2014

Solve Unknown Device Issue

f you are getting “Unknown Device“ issue.
Please follow the step by step procedure:
1) Go to Windows Registry.
2) Click “HKEY_LOCAL_MACHINE”
3) Click “SYSTEM”
4) Click “CurrentControlSet”
5) Click “Services”
6) Click “USBSTOR”
7) Check Data of “Start” (in the Right side of the Screen)
         Data is 0x00000003(3) -- USB Port works fine.
         Data is 0x00000004(4) -- USB Port doesn’t work fine.  

29 June 2014

keyword var and dynamic

A: Dynamic and var represent two completely different ideas.
var:
var essentially asks the compiler to figure out the type of the variable based on the expression on the right hand side of the assignment statement. The variable is then treated exactly as if it were explicitly declared as the type of the expression. For example the following two statements are equivalent
var a = "foo";
string a = "foo";
The key to take away here is that "var" is 100% type safe and is a compile time operation.
dynamic:
Dynamic is in many ways the exact opposite of var. Using dynamic is essentially eliminating all type safety for that particular variable. It many ways it has no type. When you call a method or field on the variable, the determination on how to invoke that field occurs at runtime. For example
dynamic d = SomeOperation();
d.Foo(); // Will this fail or not?  Won't know until you run the program
The key to take away here is that "dynamic" is not type safe and is a runtime operation.

28 June 2014

set the process priority

o set the process priority, we  can set the Process.PriorityClass property. PriorityClass is captured in the ProcessPriorityClass enumeration, which lets us set the process priority to Idle, Normal, High, AboveNormal, BelowNormal, or RealTime.   Process.BasePriority is an integer value property and is read-only.
The following table shows the relationship between the BasePriority and PriorityClass values:
BasePriority            PriorityClass
4 Idle
8 Normal
13 High
24 RealTime
Note:
In Windows 98, and the Windows Millennium Edition Platform, setting the priority class to AboveNormal or BelowNormal causes an exception to be thrown.
How do I capture a screenshot of a process’s main window?
Here is one method to take a process screenshot.  We will use the process “notepad” for this example.   Please follow these steps:

1.   Use System.Diagnostics.Process.MainWindowHandle to get the process information we require to take a screenshot.
2.   Use the Windows API, SetForegroundWindow, to activate the target process.
3.   Pass the window handle to the first parameter of the Windows API GetWindowRect, to get the location information of the target process.
4.   Define the coordinates of the process window on the screen with the location information in step 3.
5.   Use the coordinates in step 4 and Graphics.CopyFromScreen to take the screenshot.

27 June 2014

Object Initializers and Collection Initializers

Object Initializers
Object initializers provide a way to assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.
Object initializers with named types
Here we use auto-implemented properties featured in Visual C# 3.0 to define a class.  For details on auto-implemented properties, please check Auto-Implemented Properties (C# Programming Guide).
public class Point
{
    public int X { get; set; }
    public int Y { get; set; }
}
When we instantiate the objects of a Point class, we can use:
Point p = new Point();
p.X = 0;
p.Y = 1;
In Visual C# 3.0, there is a short way to achieve the same results:
// {X = 0, Y = 1} is field or property assignments
Point p = new Point { X = 0, Y = 1 };
In LINQ, we can use named object initializer like this:
The following example shows how we can use named object initializer with LINQ.  The example assumes that an object contains many fields and methods related to a product, but we are only interested in creating a sequence of objects that contain the product name and the unit price.
 var productInfos =
      from p in products
      select new { ProductName = p.ProductName, UnitPrice = p.UnitPrice };
Collection Initializers
Collection Initializers are similar in concept to Object Initializers.  They allow you to create and initialize a collection in one step.  By using a collection initializer you do not have to specify multiple calls to the Add method of the class in your source code; the compiler adds the calls.
List<int> numbers = new List<int> { 1, 100, 100 };
In fact, it is the short form of the following:
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(10);
numbers.Add(100);
Note: To be able to use a Collection Initializer on an object, the object must satisfy these two requirements:

  •          It must implement the IEnumerable interface.
  •          It must have a public Add() method.


26 June 2014

Implicit type var and Anonymous Types

                  Beginning in Visual C# 3.0, variables that are declared in the method scope can have an implicit type var.  We can use the modifier var to instruct the compiler to infer and assign the type, as shown below:
var i = 23;      // int i = 23;
var s = "Hello"; // string s = "Hello";
Arrays can also be declared with implicit typing as shown below:
var a = new[] { 1, 2, 3, 4 }; // int[]
var b = new[] { "hello", null, "world" }; // string[]
var c = new[] { a, new[] { 5, 6, 7, 8 } }; // single-dimension jagged array
Note: The following restrictions apply to implicitly-typed variable declarations:
  • var can only be used when a local variable is declared and initialized in the same statement; the variable cannot be initialized to null literal, because it does not have a type – like lambda expressions or method groups.  However, it can be initialized with an expression that happens to have the value null, as long as the expression has a type.
  • var cannot be used on fields in class scope.
  • Variables declared by using var cannot be used in their own initialization expression.  
  • In other words, var v = v++; will result in a compile-time error.
  • Multiple implicitly-typed variables cannot be initialized in the same statement.
  • If a type named var is in scope, then we will get a compile-time error if we attempt to initialize a local variable with the var keyword.
Anonymous Type
                 Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type.  The type name is generated by the compiler and is not available at the source code level.  The type of the properties is inferred by the compiler.
                The following example shows an anonymous type being initialized with two properties called Amount and Message:
var v = new { Amount = 123, Message = "Hello" };
              Anonymous types are class types that consist of one or more public read-only properties.   No other kinds of class members such as methods or events are allowed.
Note: Some rules on anonymous types:
  •  You must provide a name to a property that is being initialized with an expression, except in the situation that the second bullet describes.
  • If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the variable, field or property being used to initialize them.    
  • Anonymous types are limited to a local scope.

25 June 2014

Can base/derived classes be exported to COM


  •  COM only deals with interfaces.   Base/derived classes have no meaning or functionality in COM.   Inheritance is not applicable either.
  • In COM, interfaces can inherit from one another.   However, the .NET implementation that exports the .NET interface to COM does not support inheritance.   Therefore, you must replicate any interface members in a base interface to the derived interface.
  • Moving members between a base and derived class will have no impact on what is visible to COM.
  • Only the programmer can define what is exposed to COM.   The complier will not use reflection or anything else to determine what should be exposed.
  • All COM classes have a single, default interface.   This is the interface that is normally used for an object.   A COM class can expose other interfaces but the COM client must then query for the interface.   In .NET the first COM visible interface is used as the default interface for a COM class. 


23 June 2014

Difference between Object and Dynamic

object
This keyword is nothing more than a shortcut for System.Object, which is the root type in the C# class hierarchy
Here is a short example that demonstrates some of the benefits and problems of using the object keyword.
object obj = 10;
Console.WriteLine(obj.GetType());
obj = (int)obj + 10;
Dynamic
The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time. The dynamic type simplifies access to COM APIs such as the Office Automation APIs, and also to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM).
dynamic dyn = 10;
Console.WriteLine(dyn.GetType());
dyn = dyn + 10;
dyn = 10.0;
dyn = dyn + 10;
dyn = "10";
dyn = dyn + 10;
This is one of the main differences between object and dynamic –
with dynamic you tell the compiler that the type of an object can be known only at run time, and the compiler doesn’t try to interfere.
As a result, you can write less code.
I want to emphasize that this is no more dangerous than using the original object keyword. However, it is not less dangerous either, so all the type-checking techniques that you need to use when operating with objects (such as reflection) have to be used for dynamic objects as well.
Refer : http://blogs.msdn.com/b/csharpfaq/archive/2010/01/25/what-is-the-difference-between-dynamic-and-object-keywords.aspx

20 June 2014

use an alias for a namespace or class

Use the using directive to create an alias for a long namespace or class name. You can then use it anywhere you normally would have used that class or namespace. The using alias has a scope within the namespace you declare it in. Sample


// Namespace:
using act = System.Runtime.Remoting.Activation;
// Class:
using list = System.Collections.ArrayList;
...
list l = new list(); // Creates an ArrayList
act.UrlAttribute foo; // Equivalent to System.Runtime.Remoting.Activation.UrlAttribute foo

17 June 2014

Define variable and constant.

A variable can be defined as a meaningful name that is given to a data storage location in the computer memory that contains a value. Every variable associated with a data type determines what type of value can be stored in the variable, for example an integer, such as 100, a decimal, such as 30.05, or a character, such as ‘A’.
You can declare variables by using the following syntax:
<Data_type> <variable_name> ;


A constant is similar to a variable except that the value, which you assign to a constant, cannot be changed, as in case of a variable. Constants must be initialized at the same time they are declared. You can declare constants by using the following syntax:

const int interestRate = 10;

16 June 2014

Data Types and Types of Data Types

A data type is a data storage format that can contain a specific type or range of values. Whenever you declare variables, each variable must be assigned a specific data type. Some common data types include integers, floating point, characters, and strings. The following are the two types of data types available in .NET:

Value type - Refers to the data type that contains the data. In other words, the exact value or the data is directly stored in this data type. It means that when you assign a value type variable to another variable, then it copies the value rather than copying the reference of that variable. When you create a value type variable, a single space in memory is allocated to store the value (stack memory). Primitive data types, such as int, float, and char are examples of value type variables.

Reference type - Refers to a data type that can access data by reference. Reference is a value or an address that accesses a particular data by address, which is stored elsewhere in memory (heap memory). You can say that reference is the physical address of data, where the data is stored in memory or in the storage device. Some built-in reference types variables in .Net are string, array, and object.

15 June 2014

Which statement is used to replace multiple if-else statements in code.

                 In Visual Basic, the Select-Case statement is used to replace multiple If – Else statements and in C#, the switch-case statement is used to replace multiple if-else statements.

14 June 2014

While Vs For Loop in C#

               The while and for loops are used to execute those units of code that need to be repeatedly executed, unless the result of the specified condition evaluates to false. The only difference between the two is in their syntax. The for loop is distinguished by setting an explicit loop variable.

13 June 2014

Constants Vs Read-only variables

S.NoConstantsRead-only
1Constants are dealt with at compile-time.Read-only variables are evaluated at runtime.
2Constants supports value-type variables.Read-only variables can hold reference type variables.
3Constants should be used when it is very unlikely that the value will ever change.Read-only variables should be used when run-time calculation is required.

12 June 2014

sub-procedure Vs function

The sub-procedure is a block of multiple visual basic statements within Sub and End Sub statements. It is used to perform certain tasks, such as changing properties of objects, receiving or processing data, and displaying an output. You can define a sub-procedure anywhere in a program, such as in modules, structures, and classes.
          We can also provide arguments in a sub-procedure; however, it does not return a new value.
The function is also a set of statements within the Function and End Function statements. It is similar to sub-procedure and performs the same task. The main difference between a function and a sub-procedure is that sub-procedures do not return a value while functions do.

14 November 2013

Managed Resource vs Unmanaged Resource

Managed Resource vs Unmanaged Resource
S.NoManaged ResourceUnmanaged Resource
1Managed resources are those that are pure .NET code and managed by the runtime and are under its direct control.Unmanaged resources are those that are not. File handles, pinned memory, COM objects, database connections etc.

13 May 2013

ComboBox Vs ListBox

S.NoComboBox ListBox
1Select One DataMultiple Data.
2Facility Drop Down Facility.Drop up and Drop Down Facility
3check Box can't Use CheckBox.Can use Check Box.

LINQToSQL Vs Entity Framework

S.NoLINQTOSQLEntity Framwork
1Used rapid application developmententerprise application development.
2Support MS SQL Server database.supports all existing ADO.NET data providers.
3Relationship One to One.Many to Many.

12 May 2013

IEnumerable Vs IQuerable

S.NoIEnumerableIQuerable
1namespaceSysem.CollectionSystem.LINQ
2query datain-memory collections like List, Array etc.out-memory (like remote database, service) collections.
3suitableLINQ to Object and LINQ to XML queries.LINQ to SQL queries
4supportscustom query.custom query using CreateQuery and Execute methods
5Extension methodsIEnumerable takes functional objects.expression objects means expression tree.

09 May 2013

IEnumerable Vs Ilist


S.NoIEnumerableList
1Add/Remove Itemsdoen’t support add or remove items from the list.Supports add or remove items from the list.
2Further Filtering doesn’t support further filtering.Supports further filtering.