Archive for the 'C#' Category
Comparing Two Generic Lists in C#
Today I come across the problem while comparing two different lists. The basic requirement is finding the records which are present in one list but not in other and vise versa. I thought of sharing this piece of code which might help for you to do the same task.
Let us take the two Generic lists named timeEntryList and caseDataList. The aim is comparing these two lists and bring the results into two separate list.
1: var auditTimeEntryList =
2: timeEntryList.Where(timeEntry => !caseDataList.Any(caseData =>
3: caseData.OperatingRoom.Name.Equals(timeEntry.OperatingRoom.Name)
4: & caseData.StartDate.ToShortDateString().Equals
5: (timeEntry.ShiftDate.ToShortDateString())));
Now auditTimeEntryList contains the records which are not present in the caseDataList.
Let us write the code for bringing the records which are present in caseDataList but not in timeEntryList.
1: var auditCaseDataList = caseDataList.Where(caseData =>
2: !timeEntryList.Any(timeEntry =>
3: caseData.OperatingRoom.Area.AreaId ==
timeEntry.OperatingRoom.Area.AreaId
4: && caseData.StartDate.ToShortDateString() ==
timeEntry.ShiftDate.ToShortDateString()));
| Share this post : | ![]() |
![]() |
![]() |
![]() |
Related Posts:
No commentsVisual Studio 2010 IDE Tip–Highlight References
| Highlight References is a new feature in Visual Studio 2010 IDE which allows you visually to navigate between the references to a symbols in a opened file. This feature only available in Visual Studio 2010. | ![]() |
Highlight the symbol that you want to navigate in the opened file as shown below
Now press Ctrl + Shift + Down then your cursor shifts to all instances of the symbol in opened file. Please note this feature is available for active files that is opened files in IDE.
Related Posts:
2 commentsDocumenting C# code with XML Comments
This article explains an easy and effective way of documenting your C# code. XML comments are the solution for generating a clean documentation for your code. Visual Studio environment allows you to generate a documentation file for your project. It helps your teammates and other people who using your code.
This post will explain how to use XML comments in code and how to generate XML help documents from those comments.
XML Comment Basics
XML comments can be used to all types except namespaces. The types can be Class,Module,Interface, Enum, properties, events and methods.
XML comments are inserted inline, directly in your source code. To insert an XML comment type three single “///” immediately above definition.
Related Posts:
No commentsC# IDE Tips & Tricks Part 2
This post explains the some more tips on using Visual C# IDE to enhance developer productivity.
- Solution Configurater Right click your solution in your IDE then select the ConfigurationManager you see the following window
where you have the option to select the project for building. We can select the required project in the solution and can you can build the project.
- Solution Wide Comments You can add task lists and comments to your solution. To view the task list for your solution select view menu and click on TaskList option. Ctrl + W, T you will see the following window
You can add the task list items to window by writing the following snippet in the code file.
Related Posts:
No commentsSingleton Design Pattern in C#
Best known Creational Design Pattern is Singleton. We can implement this pattern in different ways. I am explaining some of the ways where we can implement in C#.
We will have the following concepts in implementing the Singleton pattern
- We declare a private constructor which prevents other classes to create an instantiation of the singleton class. It also prevents the sub classing.
- We declare a class as sealed so that it can not be instantiated.
- We declare a static variable for holding the reference of a singleton class.
- We declare a public property to return the singleton instance.
- We declare a method for returning the message from the singleton class.
The problem with the above code is it is not a thread –safe code. For example if two threads evaluates the if condition in the above code and both returns the true.
We will rewrite the code for thread safety
The above implementation is thread safe but having the performance issue.
Every time the lock is acquired whenever you request the object.
We can write the above code with out implementing the locks as follows
The static constructor in C# only be called whenever you create a instance for the class.
Related Posts:
No commentsC# IDE Tips & Tricks Part 1
C# Developers have been spending most their day activities
with Visual studio IDE. They may have to understand the following activities to do their job.
- Understanding Code Developer must be able analyze the relationship between classes and what API it is using for implementing the logic.
- Navigating Code Developer may or may not know where he want to navigate to the code and keep track of visited files in the code.
- Writing & Modifying Code Developers may require to modify the existing code like refractoring which involves make the code readable and fix bugs etc
- Debugging & Testing Understanding the using of debugger tools and writing unit test cases.
Visual studio provides the tools for all the activities mentioned above to make the developer efficient and productive. This post speaks about using the tools available in VS 2008 sp1. It also discusses about the tool coderush express which Microsoft partnering and allows the developers to download and use freely.
1. One of the short cut for goto definition is F12
For example you have an identifier and you want to know where it is declared just press F12. If the identifier is not your project then it shows the metadata of the identifier. eg we are viewing string class
Related Posts:
No commentsC# ?? operator
The ?? operator in C# called null-coalescing operator is used to define a default value for a nullable value types and reference types. It returns the left-hand operand if it is not null and returns right-hand operand if it is null.
example
// ?? operator example
. int? x = null;
int y = x ?? –1;
// Here the value of y will be –1.
int i = GetValue() ?? default(int);
Assigns the return value of the method to i if return value is not null other wise it assigns default value
It can be used with reference type as follows
string s = GetStringValue(); // ?? also works with reference types. // Display contents of s, unless s is null, // in which case display "Unspecified". Console.WriteLine(s ?? "Unspecified");
Related Posts:
No commentsUsing keyword in C#
Introduction
Using keyword in C# can be used as directive and as well as statement.
When you use using as Directive you will get the following advantages
- You can use the types in a namespace by declaring with using keyword.
example: using Systetm.Web;
- You can define a alias for the nested namespaces
example: using alias = CompanyName.ProjectName;
When you use using as Statement it defines the scope and allows the programmer to release the resources used by the object in the defined scope.
The object mentioned in the using statement must implement IDisposable interface. A using statement is can be exited when the end of the using statement is reached.
example :
Font font2 = new Font("Arial", 10.0f);
using (font2)
{
// use font2
}
Multiple objects can be used in with a using statement, but they must
be declared inside the using statement.
example:
using (ResourceType r1 = e1, r2 = e2, …, rN = eN) statement
it is equivalent to the following code
using (ResourceType r1 = e1)
using (ResourceType r2 = e2)…
using (ResourceType rN = eN)
statement
Related Posts:
No commentsThe Future of C#
Introduction
This post speaks about the future of C# which presented by Anders Hejlsberg chief architect of C# at PDC 2008. The coming version C#4.0 is much concerned on introducing Functional and Dynamic programming language concepts into the C#. It also speaks about C#4.0 features.
The factors that shape the C#4 are
- Declarative programming
- Dynamic programming
- Concurrence programming
Related Posts:
1 commentLambda Expressions in C# 3.0
Lambda expressions is one of the features introduced in the C# 3.0. Lambda expressions help you to ease the burden of writing verbose Anonymous Methods.
I will explain the where to use the Anonymous methods first then we see the example on lambda expressions
Anonymous Methods
Anonymous Methods is the feature in C# 2.0. The idea behind writing the anonymous methods is to write methods inline to the code with out declaring a formal named method. Normally they used for small methods that don’t require any reuse.
Example:
class TestProgram
{
static void Main( string[] args )
{
List<string> names = new List<string>();
names.Add(“Kalyan”);
names.Add(“Suresh”);
names.Add(“Naveen”);
string strResult = names.Find(IsKalyan);
}
public static bool IsKalyan(string name)
{
return name.Equals(“Kalyan”);
}
}
You can declare a anonymous method as follows
class TestProgram
{
static void Main( string[] args )
{
List<string> names = new List<string>();
names.Add(“Kalyan”);
names.Add(“Suresh”);
names.Add(“Naveen”);
string strResult = names.Find(delegate (string name )
{
return name.Equals(“Kalyan”);
} );
}
}
Advantage: It saves some typing and puts the method closer to where it is being used which helps with maintenance
Lambda Expressions
Lambda Expressions makes the thing even more easier by allowing you to write avoid anonymous method and statement block.
class TestProgram
{
static void Main( string[] args )
{
List<string> names = new List<string>();
names.Add(“Kalyan”);
names.Add(“Suresh”);
names.Add(“Naveen”);
string strResult = names.Find( name => name.Equals(“Kalyan”));
}
We can also effectively use the Lambda Expressions with LINQ and i will explain the feature in forth coming articles.




