Found 2628 Articles for Csharp

How to determine if C# .NET Core is installed?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 12:22:15

2K+ Views

The following options are for dotnet by itself. For example, dotnet −−info. They print out information about the environment if not installed it will throw error.−−infoPrints out detailed information about a .NET Core installation and the machine environment, such as the current operating system, and commit SHA of the .NET Core version.−−versionPrints out the version of the .NET Core SDK in use.−−list−runtimesPrints out a list of the installed .NET Core runtimes. An x86 version of the SDK lists only x86 runtimes, and an x64 version of the SDK lists only x64 runtimes.−−list−−sdksPrints out a list of the installed .NET Core ... Read More

What is the best way to convert seconds into (Hour:Minutes:Seconds:Milliseconds) time in C#?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 12:04:17

4K+ Views

DateTimeDateTime is a structure of value Type like int, double etc. It is available in System namespace and present in mscorlib.dll assembly.It implements interfaces like IComparable, IFormattable, IConvertible, ISerializable, IComparable, IEquatable.DateTime contains properties like Day, Month, Year, Hour, Minute, Second, DayOfWeek and others in a DateTime object.TimeSpanTimeSpan struct represents a time interval that is difference between two times measured in number of days, hours, minutes, and seconds.TimeSpan is used to compare two DateTime objects to find the difference between two dates. TimeSpan class provides FromDays, FromHours, FromMinutes, FromSeconds, and FromMilliseconds methods to create TimeSpan objects from days, hours, minutes, seconds, ... Read More

How to easily initialize a list of Tuples in C#?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 12:03:01

3K+ Views

Tuple can be used where you want to have a data structure to hold an object with properties, but you don't want to create a separate type for it. The Tuple class was introduced in .NET Framework 4.0. A tuple is a data structure that contains a sequence of elements of different data types.Tuple person = new Tuple (1, "Test", "Test1");A tuple can only include a maximum of eight elements. It gives a compiler error when you try to include more than eight elements.Tuples of Listvar tupleList = new List {    (1, "cow1"),    (5, "chickens1"),    (1, "airplane1") ... Read More

How to parse a string into a nullable int in C#?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 12:01:07

4K+ Views

C# provides a special data types, the nullable types, to which you can assign normal range of values as well as null values.C# 2.0 introduced nullable types that allow you to assign null to value type variables. You can declare nullable types using Nullable where T is a type.Nullable types can only be used with value types.The Value property will throw an InvalidOperationException if value is null; otherwise it will return the value.The HasValue property returns true if the variable contains a value, or false if it is null.You can only use == and != operators with a nullable type. ... Read More

What is an alternative to string.Replace that is case-insensitive in C#?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 12:01:40

2K+ Views

Replace() method is a string method. This method is used to replace all the specified Unicode characters or specified string from the current string object and returns a new modified string. This method can be overloaded by passing arguments to it.Example 1To replace even the case sensitive charaters Regular expressions provide a powerful, flexible, and efficient method for processing text. The extensive pattern-matching notation of regular expressions enables you to quickly parse large amounts of text to:Find specific character patterns.Validate text to ensure that it matches a predefined pattern (such as an email address).Extract, edit, replace, or delete text substrings.Add ... Read More

What are the benefits to marking a field as readonly in C#?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:58:05

424 Views

The readonly keyword is used to declare a member variable a constant, but allows the value to be calculated at runtime. This differs from a constant declared with the const modifier, which must have its value set at compile time. Using readonly you can set the value of the field either in the declaration, or in the constructor of the object that the field is a member of.The 'readonly' modifier can be used in a total of four contexts:Field declarationReadonly struct declarationReadonly member definitionRef read only method returnWhen we use the field declaration context, we need to know that the ... Read More

How to use String Format to show decimal up to 2 places or simple integer in C#?

Nizamuddin Siddiqui
Updated on 05-Nov-2020 13:56:19

3K+ Views

Converts the value of objects to strings based on the formats specified and inserts them into another string.Namespace:System Assembly:System.Runtime.dllEach overload of the Format method uses the composite formatting feature to include zero-based indexed placeholders, called format items, in a composite format string. At run time, each format item is replaced with the string representation of the corresponding argument in a parameter list. If the value of the argument is null, the format item is replaced with String.Empty.Exampleclass Program{    static void Main(string[] args){       int number = 123;       var s = string.Format("{0:0.00}", number);     ... Read More

How to convert string to title case in C#?

Nizamuddin Siddiqui
Updated on 05-Nov-2020 13:55:21

609 Views

Title case is any text, such as in a title or heading, where the first letter of major words is capitalized. Title case or headline case is a style of capitalization used for rendering the titles of published works or works of art in English.When using title case, all words are capitalized except for "minor" words unless they are the first or last word of the title.The current implementation of the ToTitleCase in the example yields an output string that is the same length as the input string.Example 1class Program{    static void Main(string[] args){       string myString ... Read More

How to set a property value by reflection in C#?

Nizamuddin Siddiqui
Updated on 05-Nov-2020 13:53:37

5K+ Views

The System. Reflection namespace contains classes that allow you to obtain information about the application and to dynamically add types, values, and objects to the application.Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in the System. Reflection namespace.Reflection allows view attribute information at runtime.Reflection allows examining various types in an assembly and instantiate these types.Reflection allows late binding to methods and properties.Reflection allows creating new types at runtime and then performs some tasks using those types.ExampleGetProperty(String)Searches for the public property with the specified name.GetType(String, Boolean)Gets ... Read More

How to rethrow InnerException without losing stack trace in C#?

Nizamuddin Siddiqui
Updated on 05-Nov-2020 13:52:36

963 Views

In c#, the throw is a keyword and it is useful to throw an exception manually during the execution of the program and we can handle those thrown exceptions using try−catch blocks based on our requirements.By using throw keyword in the catch block, we can re-throw an exception that is handled in the catch block. The re-throwing an exception is useful when we want to pass an exception to the caller to handle it in a way they want.Following is the example of re−throwing an exception to the caller using throw keyword with try-catch blocks in c#.Exampleclass Program{    static ... Read More

Advertisements