Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use List<T> or Dictionary<T,T2> in C# WinRT component

I'm trying to create a C# WinRT component for use in metro style applications (win8) and am having trouble with projected types.

It seems the IVector<> and IMap<> data types are inaccessible due to their protection level?

My Sample WinMD Library has one class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation.Collections;

namespace ClassLibrary1
{
    public sealed class Class1
    {
        public IVector<string> foo()
        {
            return new List<string>();
        }
    }
}

I get the following compiler errors:

Inconsistent accessibility: return type 'Windows.Foundation.Collections.IVector<string>' is less accessible than method 'ClassLibrary1.Class1.foo()'

'Windows.Foundation.Collections.IVector<string>' is inaccessible due to its protection level

What am I doing wrong?

EDIT:

Ah ha!

Turns out I should not be using the WinRT type names directly, but using their translated .NET names instead.

The correct code looks like this:

namespace ClassLibrary1
{
    public sealed class Class1
    {
        public IList<string> foo()
        {
            return new List<string>();
        }
    }
}
like image 926
Ty Norton Avatar asked Feb 14 '12 23:02

Ty Norton


People also ask

When to use Dictionary over List in C#?

The Find() method of the List class loops thru each object in the list until a match is found. So, if we want to look up a value using a key, then a dictionary is better for performance over the list. So, we need to use a dictionary when we know the collection will be primarily used for lookups.

How to add items to List of Dictionary in C#?

Add() Method is used to add a specified key and value to the dictionary. Syntax: public void Add (TKey key, TValue value);

What is TKey and TValue?

In Dictionary<TKey,TValue> TKey is the type of the Key, and TValue is the Type of the Value. It is recommended that you use a similar naming convention if possible in your own generics when there is nore than one type parameter.

How does C# dictionary work?

A dictionary, also called an associative array, is a collection of unique keys and a collection of values, where each key is associated with one value. Retrieving and adding values is very fast. Dictionaries take more memory because for each value there is also a key.


1 Answers

IIterable<T> is projected to .NET as IEnumerable<T>, IVector<T> is projected as IList<T>, and IMap<T> is projected as IDictionary<T>. Projection goes both ways - both for consumption and for authoring - so you should simply always be using the projected versions of those interfaces in your .NET code. When you declare a member as returning IList<T>, it will show up as IVector<T> from WinRT perspective (e.g. from C++/CX). Similarly, if you implement IDictionary on your class, it'll show up as implementing IMap in C++/CX.

If I remember correctly, you should see all types already mapped as they should be when you use Object Browser for a .NET project.

like image 165
Pavel Minaev Avatar answered Sep 30 '22 20:09

Pavel Minaev