| Event |
| Event is a kind of Notification(Information) giving one by one object to perform task. Event handling is not possible without delegate because it is delegate who will be responsible to call Event handler which even is fired. |
Event In C#
Labels:
C#.Net
Partial Class In C#
| Partial Class |
| we can break a large class code into small pieces by specifying more than one class with same but with partial keyword. In that case those same name classes will not be treated as different classes so when we will made the object of the class, we will get all the method and member for all same name class. |
Method Hiding in C#
| Method Hiding |
| We can predefined the method of parent class in child class by implementing the concept of method hiding. In method hiding the base(parent) class method is predefined in child class by specify new keyword. |
Types of delegates in c#
| Types of delegates |
Single cast delegate One delegate object can call only one method, is known as single cast delegate. Double cast delegate In c# One delegate object can call more than one method in sequence ,is known as multicast delegate. |
Labels:
C#.Net
Method Parameters in C#
| Method of Parameters |
Value Parameters in C# The Value Parameters are used to pass for passing parameters into method by value. Reference Parameters in C# Reference parameters are used to pass parameters into Method by reference. Output Parameters in C# The Output parameters are used to pass results back to the calling Method. Use of Parameter arrays in C# A Method that can handle variable number of arguments,is known as parameter arrays.Parameter arrays are declared using the keyword param. Method Overloading in C# To create More than one Method with same class name but with the different parameter lists and different definitions,is known as Method Overloading. |
Labels:
C#.Net
Solve Unknown Device Issue
| Please follow the step by step procedure |
|
Labels:
.Net Framework,
.Net Framework and Tools
keyword var and dynamic
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
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
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.string a = "foo";
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.d.Foo(); // Will this fail or not? Won't know until you run the program
Labels:
.Net Framework,
.Net Framework and Tools
Set the process priority
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:
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.
The following table shows the relationship between the BasePriority and PriorityClass values:
| S.No | BasePriority | PriorityClass |
| 1 | 4 | Idle. |
| 2 | 8 | Normal. |
| 3 | 13 | High. |
| 4 | 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.
Labels:
.Net Framework,
.Net Framework and Tools
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).
// {X = 0, Y = 1} is field or property assignments
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.
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>();
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:{
public int X { get; set; }
public int Y { get; set; }
}
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:p.X = 0;
p.Y = 1;
// {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 Initializersfrom p in products
select new { ProductName = p.ProductName, UnitPrice = p.UnitPrice };
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:numbers.Add(10);
numbers.Add(100);
- It must implement the IEnumerable interface.
- It must have a public Add() method.
Labels:
.Net Framework,
.Net Framework and Tools
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:
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:
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 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
- 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.
Labels:
.Net Framework,
.Net Framework and Tools