简体   繁体   中英

Managed and Unmanaged resources in .Net

I am confused between Managed and unmanaged resources in .net programming. I am developing a vb.net application. I read in Microsoft website that, if we use Managed resources garbage collector will dispose it and if we use unmanaged resources we need to call dispose. But I didn't get the answer to following questions anywhere.

  1. How can I differentiate the resources used by code as “Managed” and “Unmanaged”? Can I have list of resources belonging to managed and unmanaged resources?
  2. Whether the resources are used/allocated before the creation of objects or after the creation of the objects?
  3. In case of unmanaged resources whether resources will be disposed once scope gets closed or should I dispose it even after the closing of scope?
  1. if the class inherits IDisposable its unmanaged or contains some unmanaged
  2. not sure what you mean, its usually at the CTOR, so WITH the creation of the object, it cant be before, do you consider a CTOR as after? (oc unless stated otherwise)
  3. best practice would be inheriting IDisposable anywhere you use some managed code, but with .net having a good GC unless you somehow lock the unmanaged res. you should be ok.

Install-Package Microsoft.Azure.Management.ResourceManager.Fluent

         var credentials = SdkContext.AzureCredentialsFactory
                    .FromServicePrincipal(clientId, clientSecret, tenantId, 
         AzureEnvironment.AzureGlobalCloud);

         var azure = Azure
                .Configure()
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                .Authenticate(credentials)
                .WithSubscription(subscriptionID);
         List<IStorageAccount> storageAccounts = 
         azure.StorageAccounts.List().ToList();
            foreach (IStorageAccount storageaccount in storageAccounts)
            {
                var exportSecrets = true;
                CloudStorageAccount storageAccountss = new CloudStorageAccount(
                    new StorageCredentials(storageaccount.Name, 
                      storageaccount.GetKeys().FirstOrDefault().Value), true);
                var connString = storageAccountss.ToString(exportSecrets);
                var storageAccount = CloudStorageAccount.Parse(connString);
                CloudBlobClient myClient = storageAccount.CreateCloudBlobClient();
                ContainerResultSegment resultSegment = null;
                BlobContinuationToken continuationToken = null;
                resultSegment = myClient.ListContainersSegmented("", 
                  ContainerListingDetails.Metadata, 5, continuationToken, null, 
                  null);                   
                // Enumerate the containers returned.
                foreach (var container in resultSegment.Results)
                {
                    var containers = myClient.GetContainerReference(container.Name);
                    foreach (IListBlobItem item in container.ListBlobs(null, false, 
                     BlobListingDetails.None))
                    {
                        if (item.GetType() == typeof(CloudPageBlob))
                        {
                            CloudPageBlob pageBlob = (CloudPageBlob)item;
                            if (pageBlob.Name.Contains(".vhd"))
                            {
                                // 0. Unspecified  1. Locked 2. UnLocked 
                                if (Convert.ToInt32(pageBlob.Properties.LeaseStatus) 
                                 == 2)
                                {
                                    list.Add(new Disks { Name = 
                                      pageBlob.Name.ToString(), URI = 
                                            pageBlob.Uri.AbsoluteUri.ToString(), 
                                      ContainerName = container.Name.ToString() });
                                }
                            }
                        }

                    }
                }

            }
            Helper helper = new Helper();
            StringBuilder sb = new StringBuilder();
            DataTable dt = helper.GetData(list);
            foreach (DataRow dr in dt.Rows)
            {
                foreach (DataColumn dc in dt.Columns)
                    sb.Append(FormatCSV(dr[dc.ColumnName].ToString()) + ",");
                sb.Remove(sb.Length - 1, 1);
                sb.AppendLine();
            }
            File.WriteAllText("D:\\UnmanagedDisks.csv", sb.ToString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM