Found 33676 Articles for Programming

Swift: declare an empty dictionary

Nitin Aggarwal
Updated on 04-May-2023 12:26:54

211 Views

In Swift, there are some different syntaxes to declare an empty dictionary. It is important to remember that all syntaxes produce the same result. In this article, you will see examples of how to declare an empty dictionary and define a dictionary in Swift. What is a Swift Dictionary? A dictionary is a collection in Swift that lets you keep key-value pairs. Each key in the dictionary has a corresponding value, and each word in the dictionary needs to be distinct. Because dictionaries are unordered, the sequence in which key-value pairs are introduced does not matter. Example In this example, ... Read More

C# Program to Show the Use of GetEnvironmentVariable() Method of Environment Class

Sabid Ansari
Updated on 04-May-2023 13:57:23

8K+ Views

The GetEnvironmentVariable() method of the Environment class in C# is used to get the value of an environment variable. It is a useful method for retrieving system-wide and user-specific environment variables. In this article, we will discuss the usage of the GetEnvironmentVariable() method and see how to use it in a C# program. What is the GetEnvironmentVariable() Method? The GetEnvironmentVariable() method is a static method of the Environment class in C#. It takes the name of an environment variable as a string and returns the value of the variable as a string. If the environment variable does not exist, it ... Read More

Swift: Convert enum value to String?

Nitin Aggarwal
Updated on 04-May-2023 12:24:56

6K+ Views

In Swift, you can convert an enum value to a String through the rawValue property. This is if the enum has a raw value of type String. If the enum doesn't have a raw value, you can use the String(describing:) initializer to get a string representation of the enum value. Also, you can use the CustomStringConvertible protocol. Example 1 Convert an enum value to a string using the rawValue property.In this example, Fruit is an enum with a raw value of type String. The rawValue property is used to get a string representation of the myFruit enum value, which is ... Read More

C# Program to Show the Use of Exists Property

Sabid Ansari
Updated on 04-May-2023 13:55:25

2K+ Views

The Exists property in C# is a very useful property that checks whether any element in a collection matches a given condition. This property is a part of the List class in C# and returns a Boolean value indicating whether any element exists in the list that satisfies the specified condition. In this article, we will explore the usage of the Exists property in a C# program. What is the Exists Property? The Exists property is a Boolean property that is defined in the List class in C#. It takes a delegate as an argument and returns a Boolean value ... Read More

Swift: Call can throw, but it is not marked with 'try' and the error is not handled

Nitin Aggarwal
Updated on 04-May-2023 12:23:05

2K+ Views

In Swift, when you call a function that throws an error, you must either mark the function call with the try keyword or handle the error using a do-catch block. If you see the error message "Call can throw, but it is not marked with 'try' and the error is not handled", it means that you have called a function that can throw an error, but you have not handled the error properly. How to Fix These Errors? Mark the function call with the try keyword. For example − do { let result = try someFunctionThatCanThrow() ... Read More

C# Program to Show the Usage of LINQ Aggregate() Method

Sabid Ansari
Updated on 04-May-2023 13:54:03

282 Views

The Aggregate() method is a powerful LINQ method that allows you to perform a reduction operation on a sequence of elements. This method can be used to perform calculations on a collection of data, such as finding the sum, product, or maximum value of a set of numbers. In this article, we will explore how to use the Aggregate() method in a C# program. What is the Aggregate() Method? The Aggregate() method is a LINQ extension method that takes two parameters: a seed value and a function that performs a reduction operation on a sequence of elements. The seed value ... Read More

C# Program to Search Sub-Directory in a Given Directory

Sabid Ansari
Updated on 04-May-2023 13:52:41

513 Views

Searching for sub-directories in a given directory is a common task in many applications. In C#, we can use the Directory and DirectoryInfo classes provided by the System.IO namespace to perform this task. In this article, we will explore how to write a C# program to search for sub-directories in a given directory. Method: Using DirectoryInfo.GetDirectories() The simplest way to search for sub-directories in a given directory is by using the DirectoryInfo.GetDirectories() method. This method returns an array of DirectoryInfo objects that represent the directories within a specified directory. Here's how we can use the DirectoryInfo.GetDirectories() method to search for ... Read More

Swift Array Check if an index exists

Nitin Aggarwal
Updated on 04-May-2023 12:19:21

4K+ Views

In Swift, there are several ways to check whether an index exists in an array. You can use startIndex, endIndex, the indices property, and the count property. In this article, you will see some examples of checking an index. Example 1: Using FirstIndex & EndIndex You can check if a specific index exists in a Swift array by comparing the index against the startIndex and endIndex properties of the array. Here's an example. import Foundation let inputArray = [1, 2, 3, 4, 5] let targetIndex = 3 if targetIndex >= inputArray.startIndex && targetIndex < inputArray.endIndex { print("Index ... Read More

Return multiple values from a function in Swift

Nitin Aggarwal
Updated on 04-May-2023 12:17:47

3K+ Views

In Swift, you can return multiple values from a function using a tuple. In this article, you will see different examples of how to use a tuple to return multiple values from a function. Here are some more practical examples of functions that return multiple values using tuples in Swift − Example 1 - Parsing a URL into its Components import Foundation func parseURL(urlString: String) -> (scheme: String, host: String, path: String) { guard let url = URL(string: urlString) else { fatalError("Invalid URL") } return ... Read More

C# Program to Search Directories and List Files

Sabid Ansari
Updated on 04-May-2023 13:51:28

28K+ Views

Searching directories and listing files is a common task in many applications. In C#, we can use the Directory and File classes provided by the System.IO namespace to perform these tasks. In this article, we will explore how to write a C# program to search directories and list files. Method: Using Directory.GetFiles() The simplest way to search directories and list files in C# is by using the Directory.GetFiles() method. This method returns an array of strings that represents the paths of all files in a specified directory that match a specified search pattern. Here's how we can use the Directory.GetFiles() ... Read More

Advertisements