NameObjectCollectionBase 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
public ref class NameObjectCollectionBase abstract : System::Collections::ICollection
public ref class NameObjectCollectionBase abstract : System::Collections::ICollection, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
public abstract class NameObjectCollectionBase : System.Collections.ICollection
public abstract class NameObjectCollectionBase : System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Serializable]
public abstract class NameObjectCollectionBase : System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type NameObjectCollectionBase = class
interface ICollection
interface IEnumerable
type NameObjectCollectionBase = class
interface ICollection
interface IEnumerable
interface IDeserializationCallback
interface ISerializable
type NameObjectCollectionBase = class
interface ICollection
interface IEnumerable
interface ISerializable
interface IDeserializationCallback
[<System.Serializable>]
type NameObjectCollectionBase = class
interface ICollection
interface IEnumerable
interface ISerializable
interface IDeserializationCallback
Public MustInherit Class NameObjectCollectionBase
Implements ICollection
Public MustInherit Class NameObjectCollectionBase
Implements ICollection, IDeserializationCallback, ISerializable
- 상속
-
NameObjectCollectionBase
- 파생
- 특성
- 구현
예제
다음 코드 예제에서는 구현 하 고 사용 하는 방법에 설명 합니다 NameObjectCollectionBase 클래스입니다.
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
public ref class MyCollection : public NameObjectCollectionBase {
private:
DictionaryEntry^ _de;
// Creates an empty collection.
public:
MyCollection() {
_de = gcnew DictionaryEntry();
}
// Adds elements from an IDictionary into the new collection.
MyCollection( IDictionary^ d, Boolean bReadOnly ) {
_de = gcnew DictionaryEntry();
for each ( DictionaryEntry^ de in d ) {
this->BaseAdd( (String^) de->Key, de->Value );
}
this->IsReadOnly = bReadOnly;
}
// Gets a key-and-value pair (DictionaryEntry) using an index.
property DictionaryEntry^ default[ int ] {
DictionaryEntry^ get(int index) {
_de->Key = this->BaseGetKey(index);
_de->Value = this->BaseGet(index);
return( _de );
}
}
// Gets or sets the value associated with the specified key.
property Object^ default[ String^ ] {
Object^ get(String^ key) {
return( this->BaseGet( key ) );
}
void set( String^ key, Object^ value ) {
this->BaseSet( key, value );
}
}
// Gets a String array that contains all the keys in the collection.
property array<String^>^ AllKeys {
array<String^>^ get() {
return( (array<String^>^)this->BaseGetAllKeys() );
}
}
// Gets an Object array that contains all the values in the collection.
property Array^ AllValues {
Array^ get() {
return( this->BaseGetAllValues() );
}
}
// Gets a String array that contains all the values in the collection.
property array<String^>^ AllStringValues {
array<String^>^ get() {
return( (array<String^>^) this->BaseGetAllValues( String ::typeid ));
}
}
// Gets a value indicating if the collection contains keys that are not null.
property Boolean HasKeys {
Boolean get() {
return( this->BaseHasKeys() );
}
}
// Adds an entry to the collection.
void Add( String^ key, Object^ value ) {
this->BaseAdd( key, value );
}
// Removes an entry with the specified key from the collection.
void Remove( String^ key ) {
this->BaseRemove( key );
}
// Removes an entry in the specified index from the collection.
void Remove( int index ) {
this->BaseRemoveAt( index );
}
// Clears all the elements in the collection.
void Clear() {
this->BaseClear();
}
};
public ref class SamplesNameObjectCollectionBase {
public:
static void Main() {
// Creates and initializes a new MyCollection that is read-only.
IDictionary^ d = gcnew ListDictionary();
d->Add( "red", "apple" );
d->Add( "yellow", "banana" );
d->Add( "green", "pear" );
MyCollection^ myROCol = gcnew MyCollection( d, true );
// Tries to add a new item.
try {
myROCol->Add( "blue", "sky" );
}
catch ( NotSupportedException^ e ) {
Console::WriteLine( e->ToString() );
}
// Displays the keys and values of the MyCollection.
Console::WriteLine( "Read-Only Collection:" );
PrintKeysAndValues( myROCol );
// Creates and initializes an empty MyCollection that is writable.
MyCollection^ myRWCol = gcnew MyCollection();
// Adds new items to the collection.
myRWCol->Add( "purple", "grape" );
myRWCol->Add( "orange", "tangerine" );
myRWCol->Add( "black", "berries" );
Console::WriteLine( "Writable Collection (after adding values):" );
PrintKeysAndValues( myRWCol );
// Changes the value of one element.
myRWCol["orange"] = "grapefruit";
Console::WriteLine( "Writable Collection (after changing one value):" );
PrintKeysAndValues( myRWCol );
// Removes one item from the collection.
myRWCol->Remove( "black" );
Console::WriteLine( "Writable Collection (after removing one value):" );
PrintKeysAndValues( myRWCol );
// Removes all elements from the collection.
myRWCol->Clear();
Console::WriteLine( "Writable Collection (after clearing the collection):" );
PrintKeysAndValues( myRWCol );
}
// Prints the indexes, keys, and values.
static void PrintKeysAndValues( MyCollection^ myCol ) {
for ( int i = 0; i < myCol->Count; i++ ) {
Console::WriteLine( "[{0}] : {1}, {2}", i, myCol[i]->Key, myCol[i]->Value );
}
}
// Prints the keys and values using AllKeys.
static void PrintKeysAndValues2( MyCollection^ myCol ) {
for each ( String^ s in myCol->AllKeys ) {
Console::WriteLine( "{0}, {1}", s, myCol[s] );
}
}
};
int main()
{
SamplesNameObjectCollectionBase::Main();
}
/*
This code produces the following output.
System.NotSupportedException: Collection is read-only.
at System.Collections.Specialized.NameObjectCollectionBase.BaseAdd(String name, Object value)
at SamplesNameObjectCollectionBase.Main()
Read-Only Collection:
[0] : red, apple
[1] : yellow, banana
[2] : green, pear
Writable Collection (after adding values):
[0] : purple, grape
[1] : orange, tangerine
[2] : black, berries
Writable Collection (after changing one value):
[0] : purple, grape
[1] : orange, grapefruit
[2] : black, berries
Writable Collection (after removing one value):
[0] : purple, grape
[1] : orange, grapefruit
Writable Collection (after clearing the collection):
*/
using System;
using System.Collections;
using System.Collections.Specialized;
public class MyCollection : NameObjectCollectionBase
{
// Creates an empty collection.
public MyCollection() {
}
// Adds elements from an IDictionary into the new collection.
public MyCollection( IDictionary d, Boolean bReadOnly ) {
foreach ( DictionaryEntry de in d ) {
this.BaseAdd( (String) de.Key, de.Value );
}
this.IsReadOnly = bReadOnly;
}
// Gets a key-and-value pair (DictionaryEntry) using an index.
public DictionaryEntry this[ int index ] {
get {
return ( new DictionaryEntry(
this.BaseGetKey(index), this.BaseGet(index) ) );
}
}
// Gets or sets the value associated with the specified key.
public Object this[ String key ] {
get {
return( this.BaseGet( key ) );
}
set {
this.BaseSet( key, value );
}
}
// Gets a String array that contains all the keys in the collection.
public String[] AllKeys {
get {
return( this.BaseGetAllKeys() );
}
}
// Gets an Object array that contains all the values in the collection.
public Array AllValues {
get {
return( this.BaseGetAllValues() );
}
}
// Gets a String array that contains all the values in the collection.
public String[] AllStringValues {
get {
return( (String[]) this.BaseGetAllValues( typeof( string ) ));
}
}
// Gets a value indicating if the collection contains keys that are not null.
public Boolean HasKeys {
get {
return( this.BaseHasKeys() );
}
}
// Adds an entry to the collection.
public void Add( String key, Object value ) {
this.BaseAdd( key, value );
}
// Removes an entry with the specified key from the collection.
public void Remove( String key ) {
this.BaseRemove( key );
}
// Removes an entry in the specified index from the collection.
public void Remove( int index ) {
this.BaseRemoveAt( index );
}
// Clears all the elements in the collection.
public void Clear() {
this.BaseClear();
}
}
public class SamplesNameObjectCollectionBase {
public static void Main() {
// Creates and initializes a new MyCollection that is read-only.
IDictionary d = new ListDictionary();
d.Add( "red", "apple" );
d.Add( "yellow", "banana" );
d.Add( "green", "pear" );
MyCollection myROCol = new MyCollection( d, true );
// Tries to add a new item.
try {
myROCol.Add( "blue", "sky" );
}
catch ( NotSupportedException e ) {
Console.WriteLine( e.ToString() );
}
// Displays the keys and values of the MyCollection.
Console.WriteLine( "Read-Only Collection:" );
PrintKeysAndValues( myROCol );
// Creates and initializes an empty MyCollection that is writable.
MyCollection myRWCol = new MyCollection();
// Adds new items to the collection.
myRWCol.Add( "purple", "grape" );
myRWCol.Add( "orange", "tangerine" );
myRWCol.Add( "black", "berries" );
Console.WriteLine( "Writable Collection (after adding values):" );
PrintKeysAndValues( myRWCol );
// Changes the value of one element.
myRWCol["orange"] = "grapefruit";
Console.WriteLine( "Writable Collection (after changing one value):" );
PrintKeysAndValues( myRWCol );
// Removes one item from the collection.
myRWCol.Remove( "black" );
Console.WriteLine( "Writable Collection (after removing one value):" );
PrintKeysAndValues( myRWCol );
// Removes all elements from the collection.
myRWCol.Clear();
Console.WriteLine( "Writable Collection (after clearing the collection):" );
PrintKeysAndValues( myRWCol );
}
// Prints the indexes, keys, and values.
public static void PrintKeysAndValues( MyCollection myCol ) {
for ( int i = 0; i < myCol.Count; i++ ) {
Console.WriteLine( "[{0}] : {1}, {2}", i, myCol[i].Key, myCol[i].Value );
}
}
// Prints the keys and values using AllKeys.
public static void PrintKeysAndValues2( MyCollection myCol ) {
foreach ( String s in myCol.AllKeys ) {
Console.WriteLine( "{0}, {1}", s, myCol[s] );
}
}
}
/*
This code produces the following output.
System.NotSupportedException: Collection is read-only.
at System.Collections.Specialized.NameObjectCollectionBase.BaseAdd(String name, Object value)
at SamplesNameObjectCollectionBase.Main()
Read-Only Collection:
[0] : red, apple
[1] : yellow, banana
[2] : green, pear
Writable Collection (after adding values):
[0] : purple, grape
[1] : orange, tangerine
[2] : black, berries
Writable Collection (after changing one value):
[0] : purple, grape
[1] : orange, grapefruit
[2] : black, berries
Writable Collection (after removing one value):
[0] : purple, grape
[1] : orange, grapefruit
Writable Collection (after clearing the collection):
*/
Imports System.Collections
Imports System.Collections.Specialized
Public Class MyCollection
Inherits NameObjectCollectionBase
' Creates an empty collection.
Public Sub New()
End Sub
' Adds elements from an IDictionary into the new collection.
Public Sub New(d As IDictionary, bReadOnly As Boolean)
Dim de As DictionaryEntry
For Each de In d
Me.BaseAdd(CType(de.Key, String), de.Value)
Next de
Me.IsReadOnly = bReadOnly
End Sub
' Gets a key-and-value pair (DictionaryEntry) using an index.
Default Public ReadOnly Property Item(index As Integer) As DictionaryEntry
Get
return new DictionaryEntry( _
me.BaseGetKey(index), me.BaseGet(index) )
End Get
End Property
' Gets or sets the value associated with the specified key.
Default Public Property Item(key As String) As Object
Get
Return Me.BaseGet(key)
End Get
Set
Me.BaseSet(key, value)
End Set
End Property
' Gets a String array that contains all the keys in the collection.
Public ReadOnly Property AllKeys() As String()
Get
Return Me.BaseGetAllKeys()
End Get
End Property
' Gets an Object array that contains all the values in the collection.
Public ReadOnly Property AllValues() As Array
Get
Return Me.BaseGetAllValues()
End Get
End Property
' Gets a String array that contains all the values in the collection.
Public ReadOnly Property AllStringValues() As String()
Get
Return CType(Me.BaseGetAllValues(GetType(String)), String())
End Get
End Property
' Gets a value indicating if the collection contains keys that are not null.
Public ReadOnly Property HasKeys() As Boolean
Get
Return Me.BaseHasKeys()
End Get
End Property
' Adds an entry to the collection.
Public Sub Add(key As String, value As Object)
Me.BaseAdd(key, value)
End Sub
' Removes an entry with the specified key from the collection.
Overloads Public Sub Remove(key As String)
Me.BaseRemove(key)
End Sub
' Removes an entry in the specified index from the collection.
Overloads Public Sub Remove(index As Integer)
Me.BaseRemoveAt(index)
End Sub
' Clears all the elements in the collection.
Public Sub Clear()
Me.BaseClear()
End Sub
End Class
Public Class SamplesNameObjectCollectionBase
Public Shared Sub Main()
' Creates and initializes a new MyCollection that is read-only.
Dim d As New ListDictionary()
d.Add("red", "apple")
d.Add("yellow", "banana")
d.Add("green", "pear")
Dim myROCol As New MyCollection(d, True)
' Tries to add a new item.
Try
myROCol.Add("blue", "sky")
Catch e As NotSupportedException
Console.WriteLine(e.ToString())
End Try
' Displays the keys and values of the MyCollection.
Console.WriteLine("Read-Only Collection:")
PrintKeysAndValues(myROCol)
' Creates and initializes an empty MyCollection that is writable.
Dim myRWCol As New MyCollection()
' Adds new items to the collection.
myRWCol.Add("purple", "grape")
myRWCol.Add("orange", "tangerine")
myRWCol.Add("black", "berries")
Console.WriteLine("Writable Collection (after adding values):")
PrintKeysAndValues(myRWCol)
' Changes the value of one element.
myRWCol("orange") = "grapefruit"
Console.WriteLine("Writable Collection (after changing one value):")
PrintKeysAndValues(myRWCol)
' Removes one item from the collection.
myRWCol.Remove("black")
Console.WriteLine("Writable Collection (after removing one value):")
PrintKeysAndValues(myRWCol)
' Removes all elements from the collection.
myRWCol.Clear()
Console.WriteLine("Writable Collection (after clearing the collection):")
PrintKeysAndValues(myRWCol)
End Sub
' Prints the indexes, keys, and values.
Public Shared Sub PrintKeysAndValues(myCol As MyCollection)
Dim i As Integer
For i = 0 To myCol.Count - 1
Console.WriteLine("[{0}] : {1}, {2}", i, myCol(i).Key, myCol(i).Value)
Next i
End Sub
' Prints the keys and values using AllKeys.
Public Shared Sub PrintKeysAndValues2(myCol As MyCollection)
Dim s As String
For Each s In myCol.AllKeys
Console.WriteLine("{0}, {1}", s, myCol(s))
Next s
End Sub
End Class
'This code produces the following output.
'
'System.NotSupportedException: Collection is read-only.
' at System.Collections.Specialized.NameObjectCollectionBase.BaseAdd(String name, Object value)
' at SamplesNameObjectCollectionBase.Main()
'Read-Only Collection:
'[0] : red, apple
'[1] : yellow, banana
'[2] : green, pear
'Writable Collection (after adding values):
'[0] : purple, grape
'[1] : orange, tangerine
'[2] : black, berries
'Writable Collection (after changing one value):
'[0] : purple, grape
'[1] : orange, grapefruit
'[2] : black, berries
'Writable Collection (after removing one value):
'[0] : purple, grape
'[1] : orange, grapefruit
'Writable Collection (after clearing the collection):
설명
이 클래스의 기본 구조는 해시 테이블입니다.
각 요소는 키/값 쌍입니다.
의 NameObjectCollectionBase 용량은 가 보유할 수 있는 NameObjectCollectionBase 요소의 수입니다. 요소가 에 NameObjectCollectionBase추가되면 재할당을 통해 필요에 따라 용량이 자동으로 증가합니다.
해시 코드 공급자는 instance 키 NameObjectCollectionBase 에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 입니다 CaseInsensitiveHashCodeProvider.
비교자는 두 키가 같은지 여부를 결정합니다. 기본 비교자는 입니다 CaseInsensitiveComparer.
.NET Framework 버전 1.0에서 이 클래스는 문화권 구분 문자열 비교를 사용합니다. 그러나 .NET Framework 버전 1.1 이상에서 이 클래스는 문자열을 비교할 때 를 사용합니다CultureInfo.InvariantCulture. 문화권이 비교 및 정렬에 미치는 영향에 대한 자세한 내용은 Culture-Insensitive 문자열 작업 수행을 참조하세요.
null
는 키 또는 값으로 허용됩니다.
주의
메서드는 BaseGet 지정된 키를 찾을 수 없기 때문에 반환되는 값과 키와 null
연결된 값이 이므로 반환되는 null
를 구분 null
하지 않습니다.
생성자
속성
Count |
NameObjectCollectionBase 인스턴스에 포함된 키/값 쌍의 수를 가져옵니다. |
IsReadOnly |
NameObjectCollectionBase 인스턴스가 읽기 전용인지 여부를 나타내는 값을 가져오거나 설정합니다. |
Keys |
NameObjectCollectionBase.KeysCollection 인스턴스의 모든 키를 포함하는 NameObjectCollectionBase 인스턴스를 가져옵니다. |
메서드
명시적 인터페이스 구현
ICollection.CopyTo(Array, Int32) |
대상 배열의 지정된 인덱스에서 시작하여 전체 NameObjectCollectionBase을 호환되는 1차원 Array에 복사합니다. |
ICollection.IsSynchronized |
NameObjectCollectionBase 개체에 대한 액세스가 동기화되어 스레드로부터 안전하게 보호되는지 여부를 나타내는 값을 가져옵니다. |
ICollection.SyncRoot |
NameObjectCollectionBase 개체에 대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다. |
확장 메서드
Cast<TResult>(IEnumerable) |
IEnumerable의 요소를 지정된 형식으로 캐스팅합니다. |
OfType<TResult>(IEnumerable) |
지정된 형식에 따라 IEnumerable의 요소를 필터링합니다. |
AsParallel(IEnumerable) |
쿼리를 병렬화할 수 있도록 합니다. |
AsQueryable(IEnumerable) |
IEnumerable을 IQueryable로 변환합니다. |
적용 대상
스레드 보안
공용 정적 (Shared
Visual Basic의)이 형식의 멤버는 스레드로부터 안전 합니다. 인스턴스 구성원은 스레드로부터의 안전성이 보장되지 않습니다.
이 구현은 에 대해 동기화된(스레드로부터 안전한) 래퍼를 NameObjectCollectionBase제공하지 않지만 파생 클래스는 속성을 사용하여 SyncRoot 자체 동기화된 버전의 를 NameObjectCollectionBase 만들 수 있습니다.
컬렉션을 열거 되지 본질적으로 스레드로부터 안전한 프로시저가 있습니다. 컬렉션이 동기화되어 있을 때 다른 스레드에서 해당 컬렉션을 수정할 수 있으므로 이렇게 되면 열거자에서 예외가 throw됩니다. 열거하는 동안 스레드로부터 안전을 보장하려면 전체 열거를 수행하는 동안 컬렉션을 잠그거나 다른 스레드에서 변경된 내용으로 인해 발생한 예외를 catch하면 됩니다.
추가 정보
.NET