Back to Azure Sdk For

Example: Getting a subscription

sdk/resourcemanager/Azure.ResourceManager/docs/Sample1_HelloWorldAsync.md

2019-05-16T16-522.0 KB
Original Source

Example: Getting a subscription

Note: Before getting started with the samples, go through the prerequisites.

Namespaces for this example:

C#
using System;
using System.Threading.Tasks;
using Azure.Identity;
using Azure.ResourceManager.Resources;

The following code shows how to get the default subscription:

C#
ArmClient client = new ArmClient(new DefaultAzureCredential());
SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();
Console.WriteLine(subscription.Id);

It's possible to get a specific subscription as follows:

C#
string subscriptionId = "your-subscription-id";
ArmClient client = new ArmClient(new DefaultAzureCredential());
SubscriptionCollection subscriptions = client.GetSubscriptions();
SubscriptionResource subscription = await subscriptions.GetAsync(subscriptionId);
Console.WriteLine(subscription.Id);

You can also specify the default subscription when creating the ArmClient:

C#
string defaultSubscriptionId = "your-subscription-id";
ArmClient client = new ArmClient(new DefaultAzureCredential(), defaultSubscriptionId);
SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();
Console.WriteLine(subscription.Id);

With the Async suffix on methods that perform API calls, it's possible to differentiate the asynchronous and synchronous variants of any method.

From here, it is possible to get the resource groups from the retrieved subscription:

C#
ResourceGroupCollection resourceGroups = subscription.GetResourceGroups();

Next steps

Take a look at the Managing Resource Groups samples.