The lock keyword may be useful especially when you are using threading in your application. It ensures that only one thread can enter and use the critical section. If another thread needs to use the critical section, it has to wait until the previous object is released.
Application.Run in C#.Net
- The Application.Run will run the standard application message loop in the current thread where the Application runs.
- The Application.Run also takes the parameter of type Form
Labels:
C#.Net
Descendants vs. Elements in Linq to XML
Starting with Elements(XName).
Returns a filtered collection of the child elements of this element or document, in document order.
Only elements that have a matching XName are included in the collection.
And secondly the Descendants(XName) method.
Returns a filtered collection of the descendant elements for this document or element, in document order.
Only elements that have a matching XName are included in the collection.
Returns a filtered collection of the child elements of this element or document, in document order.
Only elements that have a matching XName are included in the collection.
And secondly the Descendants(XName) method.
Returns a filtered collection of the descendant elements for this document or element, in document order.
Only elements that have a matching XName are included in the collection.
Labels:
XML
Use Proper Casing for Members
| Identifier | Casing | Example |
| Class, Struct | Pascal | StrategyManager |
| Interface | Pascal with I prefix | IStrategy |
| Enumeration | Pascal | LoggingLevel |
| Enumeration Value | Pascal | Info |
| Enumeration Representing Bit Field | Pascal in plural | SearchOptions |
| Event | Pascal | Click |
| Private Field | Camel with underscore | _scheduler |
| Protected Field | Camel with underscore | _scheduler |
| Constant Field | Pascal | Pi |
| Constant Variable | Camel | definitionKey |
| Readonly Field | Camel with underscore | _listItems |
| Static Readonly Field | Pascal | ListValues |
| Variable | Camel | value |
| Method | Pascal | ComputeValue |
| Property | Pascal | Value |
| Parameter | Camel | parameter |
| Type Parameter | Pascal with T prefix | TValue |
| Namespace | Pascal | System.Drawing |
| Abbreviation with 2 and less letters | All Caps | UI, IO |
| Abbreviation with 3 and more letters | Pascal | Xml, Http |
Use Proper Class Layout
Use the following order to position all your members inside the class. The resulting layout is the combination of all the lists provided with descending priority. That means that on the top of the file there will be all public const fields followed by internal const fields.
1. Fields
2. Delegates
3. Events
4. Properties
5. Indexers
6. Constructors
7. Finalizers
8. Methods
9. Other stuff
Than follow access modifier.
1. public
2. internal
3. protected
4. private
After access modifier follow the type of the member.
1. const
2. static
3. readonly
4. nothing
The least significant sorting attribute is a declaration modifier.
1. virtual
2. abstract
3. override
4. new
Labels:
C#.Net
XSLT Elements
The links in the "Element" column point to attributes and more useful
information about each specific element.
Element
|
Description
|
Applies a template rule
from an imported style sheet
|
|
Applies a template rule
to the current element or to the current element's child nodes
|
|
Adds an attribute
|
|
Defines a named set of
attributes
|
|
Calls a named template
|
|
Used in conjunction
with <when> and <otherwise> to express multiple conditional tests
|
|
Creates a comment node
in the result tree
|
|
Creates a copy of the
current node (without child nodes and attributes)
|
|
Creates a copy of the
current node (with child nodes and attributes)
|
|
Defines the characters
and symbols to be used when converting numbers into strings, with the
format-number() function
|
|
Creates an element node
in the output document
|
|
Specifies an alternate
code to run if the processor does not support an XSLT element
|
|
Loops through each node
in a specified node set
|
|
Contains a template
that will be applied only if a specified condition is true
|
|
Imports the contents of
one style sheet into another. Note: An imported style sheet has lower precedence than the
importing style sheet
|
|
Includes the contents
of one style sheet into another. Note: An included style sheet has the same precedence as the
including style sheet
|
|
Declares a named key
that can be used in the style sheet with the key() function
|
|
Writes a message to the
output (used to report errors)
|
|
Replaces a namespace in
the style sheet to a different namespace in the output
|
|
Determines the integer
position of the current node and formats a number
|
|
Specifies a default
action for the <choose> element
|
|
Defines the format of
the output document
|
|
Declares a local or
global parameter
|
|
Defines the elements
for which white space should be preserved
|
|
Writes a processing
instruction to the output
|
|
Sorts the output
|
|
Defines the elements
for which white space should be removed
|
|
Defines the root
element of a style sheet
|
|
Rules to apply when a
specified node is matched
|
|
Writes literal text to
the output
|
|
Defines the root
element of a style sheet
|
|
Extracts the value of a
selected node
|
|
Declares a local or
global variable
|
|
Specifies an action for
the <choose> element
|
|
Defines the value of a
parameter to be passed into a template
|
Labels:
XML
Define Lambda Expression
Lambda Expression is a function without a name the calculates and returns single value.
Lambda Expression uses to lambda operator => [Goes To].
Syntax:
Add Two TestBox into Form.
TxtInput and TxtOutput.
Lambda Expression uses to lambda operator => [Goes To].
Syntax:
Inut Parameter) => Expression
Example:Add Two TestBox into Form.
TxtInput and TxtOutput.
string[] arrName ={"Lakshmi",,"Narayaanan","Sriram","Suresh","Sravan","Srinath"};
Labels:
Interview Question and Answer
Convert int? to int in C#.net
Convert int? to int in C#.net
int? v1;
int v2;
if(v1.HasValue)
v2=v1.Value
int v2;
if(v1.HasValue)
v2=v1.Value
Labels:
C#.Net
App Config File in C#.net
Read Setting Method
public static string ReadSetting(string key)
{
return ConfigurationSettings.AppSettings[key];
}
Write Setting Method
{
return ConfigurationSettings.AppSettings[key];
}
public static void WriteSetting(string key, string value)
{
XmlDocument doc = LoadConfigDocument();
XmlNode node = doc.SelectSingleNode("//appSettings");
if (node == null)
throw new InvalidOperationException("appSettings section not found in config file.");
try
{
XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
if (elem != null)
{
elem.SetAttribute("value", value);
}
else
{
elem = doc.CreateElement("add");
elem.SetAttribute("key", key);
elem.SetAttribute("value", value);
node.AppendChild(elem);
}
doc.Save(GetConfigFilePath());
}
catch
{
throw;
}
}
Remove Setting Method
{
XmlDocument doc = LoadConfigDocument();
XmlNode node = doc.SelectSingleNode("//appSettings");
if (node == null)
throw new InvalidOperationException("appSettings section not found in config file.");
try
{
XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
if (elem != null)
{
elem.SetAttribute("value", value);
}
else
{
elem = doc.CreateElement("add");
elem.SetAttribute("key", key);
elem.SetAttribute("value", value);
node.AppendChild(elem);
}
doc.Save(GetConfigFilePath());
}
catch
{
throw;
}
}
public static void RemoveSetting(string key)
{
XmlDocument doc = LoadConfigDocument();
XmlNode node = doc.SelectSingleNode("//appSettings");
try
{
if (node == null)
throw new InvalidOperationException("appSettings section not found in config file.");
else
{
node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key='{0}']", key)));
doc.Save(GetConfigFilePath());
}
}
catch (NullReferenceException e)
{
throw new Exception(string.Format("The key {0} does not exist.", key), e);
}
}
Load Config Document Method
{
XmlDocument doc = LoadConfigDocument();
XmlNode node = doc.SelectSingleNode("//appSettings");
try
{
if (node == null)
throw new InvalidOperationException("appSettings section not found in config file.");
else
{
node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key='{0}']", key)));
doc.Save(GetConfigFilePath());
}
}
catch (NullReferenceException e)
{
throw new Exception(string.Format("The key {0} does not exist.", key), e);
}
}
private static XmlDocument LoadConfigDocument()
{
XmlDocument doc = null;
try
{
doc = new XmlDocument();
doc.Load(GetConfigFilePath());
return doc;
}
catch (System.IO.FileNotFoundException e)
{ throw new Exception("No configuration file found.", e);
}
}
Get Config FilePath Method
{
XmlDocument doc = null;
try
{
doc = new XmlDocument();
doc.Load(GetConfigFilePath());
return doc;
}
catch (System.IO.FileNotFoundException e)
{ throw new Exception("No configuration file found.", e);
}
}
private static string GetConfigFilePath()
{
return Assembly.GetExecutingAssembly().Location + ".config";
}
{
return Assembly.GetExecutingAssembly().Location + ".config";
}
Labels:
C#.Net
Display ODD or Even using LINQ in C#.net
This is the C# Code for Find display Odd or Even Number using LINQ .
var numbers = from n in Enumerable.Range(1,25)
select new {number = n,OddEven =n %2 ==1? "odd","Even"};
foreach(var n in mubers)
{
listbox1.items.add("The Number is" ,+n.number +n.OddEven);
}
select new {number = n,OddEven =n %2 ==1? "odd","Even"};
foreach(var n in mubers)
{
listbox1.items.add("The Number is" ,+n.number +n.OddEven);
}
Labels:
Interview Question and Answer
Define Dynamic Resource
The resource that are referred by using the DynamicResources markup extension are knows as Dynamic Resource.
Labels:
Interview Question and Answer