03 standard class library
TRANSCRIPT
Стандартна бібліотека класів c#
1. Колекції. System.Collections, System.Collections.Generic. 2. Що таке Generic? Extension methods.3. Nullable<T>, “type?”4. Path5. DriveInfo6. Directory7. File8. Streams9. Serialization\deserializations10. Encodings11. DateTime, TimeSpan12. Regex13. Math
Зміст
Collections
Collections.Generic:
• Dictionary<TKey, TValue>
• List<T>
• Queue<T>
• Stack<T>
• LinkedList<T>
http://msdn.microsoft.com/en-us/library/system.collections.aspx - Collectionshttp://msdn.microsoft.com/en-us/library/0sbxh9x2.aspx - Collections.Generic
Collections:
• Hashtable
• ArrayList
• Queue
• Stack
Generics
http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx
• Generic methods: • FirstOrDefault<T>(T[] array)• FirstOrDefault<T>(T[] array, T defaultValue)
• Type inference:• int first = FirstOrDefault(new[] {3, 2, 1});
• default(T) expression
• Type constraints:• Base type, class, struct, new()
• Generic types:• Example: List<T>
Extension methods
http://msdn.microsoft.com/en-us/library/bb383977.aspx
Simply static methods
Used for convenience
Nullable<T>
int? = Nullable<int>
Useful properties:• HasValue• Value
http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx
Path
http://msdn.microsoft.com/en-us/library/system.io.path_methods.aspx
• Path.Combine(path1, path2)
• Path.GetDirectoryName(path)
• Path.GetFileName(path)
• Path.GetExtension(path)
• Path.GetFullPath(path)
• Path.GetRandomFileName()
• Path.GetTempFileName()
• Path.GetTempPath()
DriveInfo
http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx
• DriveInfo.GetDrives()
• drive.DriveType {CDRom, Fixed, Unknown, Network, Removable ….}
• drive.DriveFormat {NTFS, FAT32}
• drive. AvailableFreeSpace
Directory
http://msdn.microsoft.com/en-us/library/system.io.directory.aspx
• Directory.Create(folderPath)
• Directory.Move(folderPath, destinationPath)
• Directory.Delete(folderPath) && Directory.Delete(folderPath, true/*recursive*/)
• Directory.Exists(folderPath)
• Directory.GetFiles(folderPath, [search pattern])
File
http://msdn.microsoft.com/en-us/library/system.io.file.aspx
• File.Create(filePath)
• File.Move(filePath, filePathDestination)
• File.Copy(filePath, filePathDestination)
• File.Delete(filePath)
• File.Exists(filePath)
• File.WriteAllText(filePath, text)
• File.WriteAllBytes(filePath, bytes)
• File.AppendText(filePath, text)
• File.ReadAllText(filePath)
• File.ReadAllBytes(filePath)
Streams
http://msdn.microsoft.com/en-us/library/system.io.stream.aspx - Streamhttp://msdn.microsoft.com/en-us/library/system.io.filestream.aspx - FileStream
• stream.Read(data, offset, count)• stream.Write(data, offset, count)• stream.Length• stream.Seek(offset, SeekOrigin)• stream.Close()
• StreamWriter – easy way to write into text files.• StreamReader – easy way to read text from files.
• FileStream – easy way to work with binary files.
Create FileStream:• constructor (new FileStream(path, FileMode, FileAccess))• File.Create• File.Open• File.Write
Stream
MemoryStream
FileStream
CryptoStream
Binary serialization
http://msdn.microsoft.com/en-us/library/72hyey7b(v=vs.80).aspx
[Serializable]public class MyObject{ public int n1 = 0; public int n2 = 0; public String str = null;}
MyObject obj = new MyObject();obj.n1 = 1;obj.n2 = 24;obj.str = "Some String";IFormatter formatter = new BinaryFormatter();Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);formatter.Serialize(stream, obj);stream.Close();
IFormatter formatter = new BinaryFormatter();Stream stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);MyObject obj = (MyObject)formatter.Deserialize(stream);stream.Close();
Serizalization
Deserizalization
XML serialization
http://msdn.microsoft.com/en-us/library/90c86ass(v=vs.80).aspx
public class MyObject{ public int n1 = 0; public int n2 = 0; public String str = null;}
MyObject obj = new MyObject();obj.n1 = 1;obj.n2 = 24;obj.str = "Some String";XmlSerializer serializer = new XmlSerializer(typeof(MyObject));Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);serializer.Serialize(stream, obj);stream.Close();
XmlSerializer serializer = new XmlSerializer(typeof(MyObject));Stream stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);MyObject obj = (MyObject)serializer.Deserialize(stream);stream.Close();
Serizalization
Deserizalization
DateTime
• DateTime – представляє значення дати та часу.• DateTime.Add(timeSpan)• DateTime.AddDay(number)….• DateTime.ToString(format) (hh:mm:ss)• ToLocalTime()• ToUniversalTime()• DateTime.Now• DateTime.UtcNow• …
• TimeSpan – представляє інтервали дати та часу.• TimeSpan.TotalMilliseconds• TimeSpan.Days• TimeSpan.TotalDays• …
http://msdn.microsoft.com/en-us/library/system.datetime.aspx - DateTimehttp://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx - Custom DateTime Formats
Encoding
http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx
• Encoding.Default• Encoding.Unicode• Encoding.ASCII• Encoding.UTF32• …• Encoding.Convert(sourceEncoding, destinationEncoding, bytes)
• encoding.GetBytes(string)• encoding.GetString(bytes)
Regex
Email pattern: “\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b”
• Regex.IsMatch(pattern, string)
• Regex.Replace(pattern, string, newValue)
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx
Math
http://msdn.microsoft.com/en-us/library/system.math.aspx
• Math.Abs(number)
• Math.Pow(number)
• Math.Sin(angle)
• Math.Cos(angle)
• Math.Max(number1, number2)
• Math.Min(number1, number2)
• …