We can not declare Async method in interface or it's not needed as Async is code marker for compiler it has no relation with overriding feature. If we want to declare Async methods in Interface we need to set return type of method to Task/Task
C#, WPF, ASP.Net core | Api Versioning |Content Negotiation | Rate limiting |Middleware | Dependency injection | Routing | .Net core | Filters
Thursday, May 9, 2019
C# : How to declare Async method in Interface
We can not declare Async method in interface or it's not needed as Async is code marker for compiler it has no relation with overriding feature. If we want to declare Async methods in Interface we need to set return type of method to Task/Task
C# : Can we return value from events?
Yes, we can return value from events but returned value will be available to the publisher of events.
Generally, publisher do not want any value from subscriber of event, he wants to notify subscriber, so the subscriber can perform appropriate action on the particular event. for example Button click, Button class is publisher of event and allowing subscriber to perform some action when button clicked. Yes, but if we will talk technically and tried to find out answer of question "can we return value from event so, answer will be yes. Please see below example :
namespace MyLibrary
{
///
/// This class will show us use of delegate.
///
public class ReturnValuefromEvent
{
public event LoopConditionChangingEvent LoopConditionChanging;
// This method will run loop till given condition by user and when i and conition are equal then it will ask to client, that still he wants to continue..."
// by generating events and event will return value to the publisher of event.
// hence conclusion is that event can return value back to the publisher.
public void RunLoop(int condition)
{
for (int i = 0; i <= condition; i++)
{
//here we are doing some operation like :
Console.Out.WriteLine("value of i=" + i);
if (i == condition && condition != 100)
// here we are checking if loop is reached to it's initial condition provided by user, we will ask to user are you still want to continue.
{ // and also we are setting maximum limit of loop i.e.100.
if (LoopConditionChanging != null)
{
if (LoopConditionChanging())//Callback--asking to client and if client will tell yes we will increment loop
{
condition = condition + 10;
}
}
}
}
}
}
}
user will subscribe "LoopConditionChangingEvent " which will return bool value (true or false) that publisher will use inside of loop and according to given value either loop will continue or terminated.
Friday, April 12, 2019
WPF: Style definition and use
WPF : Style Introduction :
Style are way to set common property of a control at one place to reuse it.
Style are similar to CSS classes in HTML/Web Applications.
We can declare Style at multiple places like :
Resource Section of Control or it's Parent.(Scope :Control it self and it's child can use this Style)
Resource Section of Window.(Scope =containing form only)
Inside of Resource Dictionary.(Scope = Application level).
Below are some common attributes of Style:
TargetType :if we are setting only TargetType then Style will be application to all controls mention as Target type in applicableScope.
x:Key : used to declare a key, if we declare a Key then style will not be applied on controls automatically, we need to set explicitly.
BasedOn : used to create a new Style by inheriting from an existing Style.
Note : Control level Properties have higher precedence than Style. i.e. if you have applied style which is setting background color of control and you also have set background color at Control level, Color set at control will be applicable and system will ignore color what you mentioned in Style.
Thursday, April 11, 2019
WPF : How to use POCO object in XAML file and use converter in WPF
I am trying to use CLR object (Person) in XAML file and also demonstrating use of converter.
This example is just to demonstrate how to use above mentioned functionality, it's doesn't perform any meaningful functionality.
I have imported namespace for Person and Converter classes and created resource in Window for both classes (Note : We can initialize classes via resource only if class has a default constructor).
Definition of person and converter classes are as follows :
Wednesday, April 10, 2019
SQL Server : Some common query optimization techniques
Try to fetch relevant data as per your needs from SQL Server, so server need to consume resources for unwanted data.
Always provide column names in SELECT clause and provide only required column list.
Try to avoid DISTINCT clause, one work around to eliminate distinct is to add a unique/primary key column in result, so you will be able to decide weather record is duplicate of not based on different value of primary/unique key.
Try to avoid cross joins.
if you are grouping data and applying having clause try to put criteria in where clause if possible, as group by and having will be applied on result of query so you can limit the no. of rows by applying filter criteria in where clause so you will get lesser record and system will be able to perform grouping operation fast.
use Like clause carefully and efficiently as it will be more resource intensive for server and produce more unwanted records in query result. for ex if you want to search city name start from 'MA' use LIKE 'MA%' instead of LIKE '%MA%'.
Avoid Co-related sub queries and use Join instead, for ex :
SELECT c.Name, c.City,(SELECT
CompanyName FROM Company WHERE ID = c.CompanyID) AS CompanyName
FROM Customer c
you can achieve same result by using JOIN which will be faster than this.
If you want to check if a record exists, use EXISTS() instead of COUNT() because count will scan entire table while Exist will return when matching record found. for ex: IF( SELECT COUNT(NAME) FROM PERSON WHERE AGE>100) >0) do something...instead use IF(EXISTS(SELECT NAME FROM PERSON WHERE AGE>100)
Monday, April 8, 2019
SQL Server : Generating xml from Query
Below query will generate xml for employee records
SELECT [EMP_ID]
,[FIRST_NAME]
,[LAST_NAME]
,[DEPARTMENT_ID]
,[SALARY]
FROM [ABCD].[dbo].[Employee]
FOR XML RAW('Employee'), ROOT('Employees'), ELEMENTS XSINIL;
SELECT [EMP_ID]
,[FIRST_NAME]
,[LAST_NAME]
,[DEPARTMENT_ID]
,[SALARY]
FROM [ABCD].[dbo].[Employee]
FOR XML RAW('Employee'), ROOT('Employees'), ELEMENTS XSINIL;
SQL Server : Delete duplicate rows from table
Delete duplicate rows from table , we are considering duplicate rows based on Name and City column Values.
--Works well if Table Desn't have primary key
WITH TEMP AS (
SELECT ROW_NUMBER() OVER ( PARTITION BY NAME,CITY ORDER BY NAME,CITY) as RN,NAME,CITY
FROM TAbleDuplicate
)
DELETE FROM TEMP WHERE
RN>1
Another way to delete duplicate from table (if primary key is present in table):
DELETE FROM EMPLOYEE WHERE EMP_ID NOT IN (
SELECT Min(EMP_ID) FROM EMPLOYEE
GROUP BY FIRST_NAME,LAST_NAME,SALARY )
Subscribe to:
Posts (Atom)
C# Record type: Something to remember while using record types
Record in c# provide a concise and expressive way to create immutable data types, record is a keyword in c#, we can use this keyword with ...
-
Stack implementation in C# public class Stack { readonly int _capacity; int top; int[] _stack ; ...
-
Asynchronous programming makes it possible to run multiple operation concurrently, In other words Asynchronously. Dot Net framework 4.0 s...
-
A variable of type Product?[] denotes an array that can contain Product or null values but that won’t be null itself: Product?[] arr1 = new...



