Found 34494 Articles for Programming

Find all substrings in a string using C#

karthikeya Boyini
Updated on 21-Jun-2020 14:25:18

356 Views

Use the substring() method in C# to find all substrings in a string.Let’s say our string is −pqrLoop through the length of the string and use the Substring function from the beginning to the end of the string −for (int start = 0; start

File Handling in C#

George John
Updated on 21-Jun-2020 14:34:14

572 Views

A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream.In C#, you need to create a FileStream object to create a new file or open an existing file. The syntax for creating a FileStream object is as follows −FileStream = new FileStream( , , , );Here, the file operations are also included as shown below −The FileMode enumerator defines various methods for opening files. The members of the FileMode enumerator are −Append − It opens an existing ... Read More

What is the System.Reflection.Module in C#?

Chandu yadav
Updated on 21-Jun-2020 14:18:33

208 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.It has a module constructor that initializes a new instance of the Module class. A module is a portable executable file that has one or more classes and interfaces.Let us see an example of System.Reflection in C# −Exampleusing System; using System.Reflection; [AttributeUsage(AttributeTargets.All)] public class HelpAttribute : System.Attribute {    public readonly string Url;    public string Topic // Topic is a named parameter {       get {          return ... Read More

How to define character constants in C#?

Chandu yadav
Updated on 21-Jun-2020 13:50:27

195 Views

Character literals are enclosed in single quotes. For example, 'x' and can be stored in a simple variable of char type. A character literal can be a plain character (such as 'x'), an escape sequence (such as '\t'), or a universal character (such as '\u02C0').Let us see an example how to define a character constant in C# −using System; namespace Demo {    class Program {       static void Main(string[] args) {          Console.WriteLine("Welcome!\t");          Console.WriteLine("This is it!");          Console.ReadLine();       }    } }Above, we ... Read More

Variable Arguments (Varargs) in C#

Arjun Thakur
Updated on 21-Jun-2020 13:59:58

4K+ Views

Use the param keyword to get the variable arguments in C#.Let us see an example to multiply integers. We have used params keyword to accept any number of int values −static int Multiply(params int[] b)The above allows us to find multiplication of numbers with one as well as two int values. The fllowing calls the same function with multiple values −int mulVal1 = Multiply(5); int mulVal2 = Multiply(5, 10);Let us see the complete code to understand how variable arguments work in C# −Exampleusing System; class Program {    static void Main() {       int mulVal1 = Multiply(5); ... Read More

How to create a Directory using C#?

Samual Sam
Updated on 21-Jun-2020 14:00:14

251 Views

To create, move and delete directories in C#, the System.IO.Directory class has methods.Firstly, import the System.IO namespace.Now, use the Director.CreateDirectory() method to create a directory in the specified path −string myDir = @"D:\NEW"; if (!Directory.Exists(myDir)) {    Directory.CreateDirectory(myDir); }In the same way, you can create a sub-directory −string mysubdir = @"C:\NEW\my\"; Directory.CreateDirectory(mysubdir);

Value Type vs Reference Type in C#

Ankith Reddy
Updated on 21-Jun-2020 14:01:19

5K+ Views

Value Type and Reference, both are types in C# −Value TypeValue type variables can be assigned a value directly. They are derived from the class System.ValueType. The value types directly contain data. When you declare an int type, the system allocates memory to store the value.Value Type variables are stored in the stack.Examples are int, char, and float, which stores numbers, alphabets, and floating point numbers, respectively.Reference TypeIt refers to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the ... Read More

How to create a Dictionary using C#?

karthikeya Boyini
Updated on 21-Jun-2020 14:02:04

157 Views

Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.To create a dictionary, you first need to set it and add the key and values. Here we have added 5 keys with values to a Dictionary. We have set its keys and value type as int.IDictionary d = new Dictionary(); d.Add(1, 44); d.Add(2, 34); d.Add(3, 66); d.Add(4, 47); d.Add(5, 76);The following is the complete code −Exampleusing System; using System.Collections.Generic; public class Demo {    public static void Main() {       IDictionary d = new Dictionary();       d.Add(1, 44); ... Read More

How to count the number of items in a C# list?

George John
Updated on 21-Jun-2020 13:35:06

579 Views

Use the Array.Count property in C# to count the number of items in a list in C# −Set the listList myList = new List() {    "electronics",    "clothing",    "appliances",    "accessories" };Now count the number of items in a list in C# −myList.CountThe following is the complete code to count the number of items in a list −Exampleusing System; using System.Collections.Generic; class Program {    static void Main() {       List myList = new List() {          "electronics",          "clothing",          "appliances",          "accessories"       };       Console.WriteLine(myList.Count);    } }

What is the difference between function overriding and method hiding in C#?

Samual Sam
Updated on 21-Jun-2020 13:36:43

520 Views

OverridingUnder overriding, you can define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement.Let us see an example of abstract classes that implements Overriding −Exampleusing System; namespace PolymorphismApplication {    abstract class Shape {       public abstract int area();    }    class Rectangle: Shape {       private int length;       private int width;       public Rectangle( int a = 0, int b = 0) {          length = a;       ... Read More

Advertisements