जॉन पामर के सही जवाब पर थोड़ा विस्तार करने के लिए है चलाने के नीचे छोड़कर, यहां एक एफ # इंटरएक्टिव सत्र कि दिखाता है कि कैसे int list
और List<int>
है समानार्थक हैं, ठीक उसी समय तक जब वे नहीं हैं। और ध्यान दें कि वहाँ भी है list<int>
और अतिरिक्त भ्रम के लिए ResizeArray<int>
:
F# Interactive for F# 4.0 (Open Source Edition)
Freely distributed under the Apache 2.0 Open Source License
For help type #help;;
> typeof<list<int>>.Namespace ;;
val it : string = "Microsoft.FSharp.Collections"
> typeof<list<int>>.Name ;;
val it : string = "FSharpList`1"
> typeof<List<int>>.Namespace ;;
val it : string = "Microsoft.FSharp.Collections"
> typeof<List<int>>.Name ;;
val it : string = "FSharpList`1"
> typeof<int list>.Namespace ;;
val it : string = "Microsoft.FSharp.Collections"
> typeof<int list>.Name ;;
val it : string = "FSharpList`1"
> typeof<ResizeArray<int>>.Namespace ;;
val it : string = "System.Collections.Generic"
> typeof<ResizeArray<int>>.Name ;;
val it : string = "List`1"
-
- printfn "Now we'll open System.Collections.Generic. Watch what happens."
- ;;
Now we'll open System.Collections.Generic. Watch what happens.
val it : unit =()
> open System.Collections.Generic ;;
> typeof<list<int>>.Namespace ;;
val it : string = "Microsoft.FSharp.Collections"
> typeof<list<int>>.Name ;;
val it : string = "FSharpList`1"
> typeof<List<int>>.Namespace ;;
val it : string = "System.Collections.Generic"
> typeof<List<int>>.Name ;;
val it : string = "List`1"
> typeof<int list>.Namespace ;;
val it : string = "Microsoft.FSharp.Collections"
> typeof<int list>.Name ;;
val it : string = "FSharpList`1"
> typeof<ResizeArray<int>>.Namespace ;;
val it : string = "System.Collections.Generic"
> typeof<ResizeArray<int>>.Name ;;
val it : string = "List`1"
तो List<int>
एक अपरकेस एल एफ # सूची प्रकार (एक सिर सूचक के साथ एक अपरिवर्तनीय लिंक्ड सूची है, जो हे (1) सिर का उपयोग और है हो जाएगा के साथ पूर्ववत, लेकिन ओ (एन) पूंछ का उपयोग और संलग्न) यदि आपने System.Collections.Generic
नामस्थान खोला नहीं है। लेकिन यदि आप में हैं, तो अचानक List<int>
.NET System.Collections.Generic.List<T>
क्लास का हल करता है, जो म्यूटेबल डेटा संरचना को ओ (1) लुकअप के साथ कहीं भी, एमोरेटेड ओ (1) संलग्न करता है, लेकिन ओ (एन) प्रीपेन्ड करता है। तो यह वास्तव में मामलों है जिसका आप उपयोग कर रहे हैं।
सुरक्षित पक्ष पर रहने के लिए, यदि आप एफ # सूची संरचना का उपयोग करना चाहते हैं, तो मैं इसे int list
(मेरी पसंद से अंग्रेजी की तरह पढ़ता है) या list<int>
(जो कुछ लोग पसंद करते हैं क्योंकि यह सी #)। जब आप नेट नामस्थान खोलते हैं तो उनमें से कोई भी अचानक एक अलग अर्थ प्राप्त नहीं करेगा; वे एफ # सूची संरचना का उल्लेख करना जारी रखेंगे। और F # सूचियों को संदर्भित करने के लिए List<int>
का उपयोग करने से बचें; केवल तब उपयोग करें जब आपने System.Collections.Generic
नामस्थान खोला है और आप .NET System.Collections.Generic.List<T>
उदाहरण प्राप्त करना चाहते हैं। अंत में, ध्यान दें कि F #, System.Collections.Generic.List<T>
में System.Collections.Generic
नामस्थान खोलने के बिना एक प्रकार का उपनाम उपलब्ध है। डिफ़ॉल्ट रूप से, किसी भी नामस्थान को खोलने की आवश्यकता के बिना, आप इस प्रकार को ResizeArray<T>
नाम से एक्सेस कर सकते हैं।
सारांश:
सुरक्षित प्रकार के नाम:
int list
और list<int>
(हमेशा एफ # अकेले लिंक्ड सूची प्रकार का उल्लेख)
ResizeArray<int>
(हमेशा सी # System.Collections.Generic.List<T>
प्रकार को संदर्भित करता है)
असुरक्षित प्रकार के नाम क्योंकि उनके अर्थ क्या नाम स्थान आपके द्वारा खोले गए के आधार पर परिवर्तन:
List<int>
(करने के लिए पहली बार में अकेले लिंक्ड सूची प्रकार एफ # संदर्भित करता है, लेकिन परिवर्तन सी # प्रकार के, अर्थात यदि आप खोलने System.Collections.Generic
नामस्थान)। एक सामान्य नियम के रूप में, इस प्रकार के नाम का उपयोग न करें; यदि आप इस प्रकार को एफ # में चाहते हैं, तो इसके बजाय ResizeArray<int>
का उपयोग करें।
शायद यह कहने के लिए एक हिस्सा जोड़ें कि 'System.Collections.Generic.List' और साथ ही एक उपनाम 'ResizeArray'' और इसलिए उस विशिष्ट चीज़ के लिए किसी को नामस्थान –
Sehnsucht
@Sehnsucht खोलने की आवश्यकता नहीं है - अच्छा विचार! किया हुआ। – rmunn