Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by karthikeya Boyini
Page 9 of 143
How to write the first program in C#?
Writing your first program in C# is the foundation of learning this powerful programming language. A C# program consists of several key components that work together to produce output. Let's explore the structure and elements of a basic C# program. Syntax Following is the basic structure of a C# program − using System; namespace NamespaceName { class ClassName { static void Main(string[] args) { // Program logic here ...
Read MoreFlow control in try catch finally in C#
The flow control in try-catch-finally blocks in C# follows a specific execution order depending on whether exceptions occur. Understanding this flow is crucial for proper error handling and resource management in your applications. Syntax Following is the syntax for try-catch-finally blocks − try { // Code that may throw an exception } catch (ExceptionType e) { // Exception handling code } finally { // Code that always executes } Flow Control Rules The try block executes first and contains code that might ...
Read MoreC# program to find the index of an element in a List
In C#, you can find the index of an element in a List using the IndexOf() method. This method returns the zero-based index of the first occurrence of the specified element, or -1 if the element is not found. Syntax Following is the syntax for the IndexOf() method − int IndexOf(T item) int IndexOf(T item, int startIndex) int IndexOf(T item, int startIndex, int count) Parameters item: The element to locate in the list. startIndex: The zero-based starting index of the search (optional). count: The number of elements to search (optional). ...
Read MoreHow to declare and initialize a dictionary in C#?
A Dictionary in C# is a collection that stores key-value pairs, where each key must be unique. It is part of the System.Collections.Generic namespace and provides fast lookups based on keys. Syntax Following is the syntax for declaring and initializing a Dictionary − Dictionary dictionaryName = new Dictionary(); Where TKey is the type of the key and TValue is the type of the value. You can also use the interface type for more flexibility − IDictionary dictionaryName = new Dictionary(); Using Dictionary.Add() Method The Add() method allows you ...
Read MoreC# Program to find the smallest element from an array using Lambda Expressions
Lambda expressions in C# provide a concise way to write anonymous functions. When combined with LINQ methods like Min(), they offer powerful tools for array operations. This article demonstrates how to find the smallest element from an array using lambda expressions. Syntax The basic syntax for using Min() with lambda expressions − array.Min() // finds minimum value directly array.Min(element => expression) // finds minimum based on expression Using Min() Without Lambda Expression For ...
Read MoreWhat are abstract classes in C#?
An abstract class in C# is a class that cannot be instantiated directly and contains one or more abstract methods that must be implemented by derived classes. Abstract classes provide a common base with shared functionality while requiring derived classes to implement specific methods with their own specialized behavior. Abstract classes are declared using the abstract keyword and serve as blueprints for related classes that share common characteristics but need different implementations for certain operations. Syntax Following is the syntax for declaring an abstract class with abstract methods − abstract class ClassName { ...
Read MorePrivate Variables in C#
The private access specifier in C# allows a class to hide its member variables and member functions from other functions and objects. Only methods of the same class can access its private members. Even an instance of a class cannot access its private members directly from outside the class. Syntax Following is the syntax for declaring a private variable − private dataType variableName; For example − private double length; private int count; private string name; Key Rules of Private Variables Private members can only be accessed within ...
Read MoreC# program to get the List of keys from a Dictionary
In C#, you can extract all the keys from a Dictionary as a List using the Keys property. This is useful when you need to work with dictionary keys as a separate collection or perform operations like sorting, filtering, or iteration. Syntax Following is the syntax to get keys from a Dictionary − Dictionary dictionary = new Dictionary(); List keys = new List(dictionary.Keys); You can also use LINQ to convert keys to a List − List keys = dictionary.Keys.ToList(); Using Dictionary.Keys Property The Keys property returns a collection of ...
Read MoreHow to declare and initialize constant strings in C#?
To declare a constant string in C#, use the const keyword. Constants are immutable values that are set at compile-time and cannot be changed during program execution. Once initialized, attempting to modify a constant will result in a compile-time error. Syntax Following is the syntax for declaring and initializing a constant string − const string constantName = "value"; Constants must be initialized at the time of declaration and the value must be a compile-time constant expression. Key Rules for Constants Constants must be initialized at declaration − you cannot declare ...
Read MoreInsert more than one element at once in a C# List
The InsertRange() method in C# allows you to insert multiple elements into a List at a specified position. This method is useful when you need to add a collection of elements at once rather than adding them one by one. Syntax Following is the syntax for the InsertRange() method − public void InsertRange(int index, IEnumerable collection) Parameters index − The zero-based index at which the new elements should be inserted. collection − The collection whose elements should be inserted into the List. The collection itself cannot be null, but ...
Read More