RSS

Indexer-Supported Collection in C#

Indexer is a faster way to search a list / collection in C#, no looping is necessary.

Classes that support indexer:

  • Array []
  • List<T>
  • Dictionary<Key, Value>
  • ILookup<Key, Value>

Classes that don’t support indexer:

  • IEnumerable<T>
  • HashSet<T>
  • ICollection<T>

For classes that don’t support indexer, there’s ElementAt LINQ extension method.

Advertisements
 
Leave a comment

Posted by on September 28, 2017 in General

 

Tags: , ,

Why Must Extension Methods Be Declared in a Static Class?

Some possible answers:

  • The purpose of Extension Methods is to make LINQ work and LINQ only needs extension methods to be in a static, non-generic, non-nested class.
  • Easier for compiler to work with. Compiler and VS’s Intellisense can easily locate the methods.
  • Grouping the helper methods together.

Sources:
https://stackoverflow.com/questions/3930335/why-are-extension-methods-only-allowed-in-non-nested-non-generic-static-class

https://stackoverflow.com/questions/7163823/why-must-c-sharp-extension-methods-be-defined-in-static-classes

 
Leave a comment

Posted by on September 20, 2017 in General

 

Tags: ,

Mocking Generic Method with NSubstitute

The generic method:


public interface ICacheService
{
    T Get<T>(string id);
}

User object in NSubstitute to mock the generic method.


private ICacheService MockICacheService()
{
    var service = Substitute.For<ICacheService>();

    // Return Models
    object returnForAny = null;

    // Mock
    service.Get<object>(Arg.Any<string>()).ReturnsForAnyArgs(returnForAny);

    return service;
}

 
Leave a comment

Posted by on September 13, 2017 in General

 

Tags: ,

Azure Storage Blob Listing Query with Search Pattern

Azure Storage blob is not behaving like files and folders in your local computer, albeit looking like one. So, there’s no support for search pattern, such as *.txt

The solution is to write your own search pattern.

from https://stackoverflow.com/questions/30299671/matching-strings-with-wildcard


public static String WildCardToRegular(String value) {
    return "^" + Regex.Escape(value).Replace("\\*", ".*") + "$";
}

Then, using it in Listing the blob:


var blobList = await container.ListBlobsSegmentedAsync(blobFilePath, true, BlobListingDetails.None, 1000, token, null, null);
var items = blobList.Results.Select(x => x as CloudBlockBlob);

// Filter items by search pattern, if specify
if (!string.IsNullOrEmpty(searchPattern))
{
    items = items.Select(i =>
    {
        var filename = Path.GetFileName(i.Name);
        if (Regex.IsMatch(filename, WildCardToRegular(searchPattern), RegexOptions.IgnoreCase))
        {
            return i;
        }
	return null;
    }).ToList();
}

 
Leave a comment

Posted by on September 9, 2017 in General

 

Tags: ,

Azure Active Directory IDs

Setting up or accessing Azure Active Directory require many IDs. Below is how to find them:

Tenant ID
Azure Portal > Azure AD > Domain names
Azure Portal > Azure AD > Properties > Directory ID

Instance
https://login.windows.net/

Client ID / Application ID / Audience
Azure Portal > Azure AD > App registrations > ApplicationId

Client Secret / Application Secret
Azure Portal > Azure AD > App registrations > choose the application > Keys
Keys are hidden and only visible the first time you add them.

Redirect URIs or Reply URLs
Azure Portal > Azure AD > App registrations > choose the application > Settings > Redirect URIs or Reply URLs

Resource
Azure Portal > Azure AD > App registrations
SQL database: https://database.windows.net/

References:
Azure AD

 
Leave a comment

Posted by on August 17, 2017 in General

 

Tags: ,

Azure Key Vault PowerShell Commands

Login-AzureRmAccount

# List all Key vaults
Get-AzureRmKeyVault

# Get a key given the secret name.
Get-AzureKeyVaultKey -VaultName "vault-name" -Name "secret-name"

# List all secrets
Get-AzureKeyVaultSecret -VaultName "vault-name"

# Get a secret
Get-AzureKeyVaultSecret -VaultName "vault-name" -Name "secret-name"

Reference:
AzureRM.KeyVault
Azure Get Started

 
Leave a comment

Posted by on August 17, 2017 in General

 

Tags: , ,

Does ConcurrentDirectory Value Get Locked Automatically?

According to MSDN doc, lock is not necessary with ConcurrentDictionary. Does the underlying object in the dictionary also gets locked automatically?

var items = new ConcurrentDictionary<int, List<Item>>();

public class Item
{
    public string Name {get;set;}
    public double Cost {get;set;}
}
 
Leave a comment

Posted by on August 15, 2017 in General

 

Tags: , ,

 
%d bloggers like this: