Found 34488 Articles for Programming

What is the difference between VAR and DYNAMIC keywords in C#?

Samual Sam
Updated on 22-Jun-2020 13:12:59

486 Views

DynamicStore any type of value in the dynamic data type variable created using dynamic keyword. Type checking for these types of variables takes place at run-time. Dynamic are dynamically typed variables.The following is the syntax for declaring a dynamic type −dynamic = value;The following is an example −dynamic val1 = 100; dynamic val2 = 5; dynamic val3 = 20;The dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at runtime.VarThe "var" keyword initializes variables with var support. Just assign ... Read More

How to compute Average for the Set of Values using C#?

karthikeya Boyini
Updated on 22-Jun-2020 13:13:36

188 Views

Firstly, set an array with values −int[] myArr = new int[] {    34,    23,    77,    67 };To get the average, firstly get the sum of array elements.Divide the sum with the length of the array and that will give you the average of the elements −int sum = 0; int average = 0; for (int i = 0; i < len; i++) {    sum += myArr[i]; } average = sum / len;The following is the complete code to get the array in C# −Example Live Demousing System; public class Program {    public static void Main() ... Read More

How to compile unsafe code in C#?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

2K+ Views

For compiling unsafe code, you have to specify the /unsafe command-line switch with command-line compiler. For example, to compile a program named one.cs containing unsafe code, from command line, give the command − csc /unsafe one.cs Under Visual Studio IDE, enable use of unsafe code in the project properties. The following are the steps − Open Project properties by double clicking the properties node in the Solution Explorer. Click on the “Build” tab. Select the option "Allow unsafe code".

File Searching using C#

Samual Sam
Updated on 22-Jun-2020 13:14:39

492 Views

To search files from the list of files in a directory, try to run the following code −Exampleusing System; using System.IO; namespace Demo {    class Program {       static void Main(string[] args) {          //creating a DirectoryInfo object          DirectoryInfo mydir = new DirectoryInfo(@"d:\amit");          // getting the files in the directory, their names and size          FileInfo [] f = mydir.GetFiles();          foreach (FileInfo file in f) {             Console.WriteLine("File Name: {0} Size: ... Read More

Make your collections thread-safe in C#

Prabhas
Updated on 30-Jul-2019 22:30:23

265 Views

The .NET Framework 4 brought the System.Collections.Concurrent namespace. This has several collection classes that are thread-safe and scalable. These collections are called concurrent collections because they can be accessed by multiple threads at a time. The following concurrent collection types use lightweight synchronization mechanisms: SpinLock, SpinWait, etc. These are new in .NET Framework 4. Let us see the concurrent collection in C# − Type Description BlockingCollection Bounding and blocking functionality for any type. ConcurrentDictionary Thread-safe implementation of a dictionary of key-value pairs. ConcurrentQueue Thread-safe implementation of a FIFO (first-in, first-out) queue. ConcurrentStack Thread-safe ... Read More

Managed code vs Unmanaged code in C#

Prabhas
Updated on 22-Jun-2020 13:15:33

3K+ Views

Unmanaged CodeApplications that are not under the control of the CLR are unmanagedThe unsafe code or the unmanaged code is a code block that uses a pointer variable.The unsafe modifier allows pointer usage in unmanaged code.Let us see the example −Examplestatic unsafe void Main(string[] args) {    int var = 20;    int* p = &var;    Console.WriteLine("Data is: {0} ", var);    Console.WriteLine("Address is: {0}", (int)p);    Console.ReadKey(); }Managed CodeManaged code is a code whose execution is managed by Common Language Runtime. It gets the managed code and compiles it into machine code. After that, the code is executed.The ... Read More

Matching strings with a wildcard in C#

varma
Updated on 22-Jun-2020 13:15:59

3K+ Views

Commonly used wildcard characters are the asterisk (*). It represents zero or more characters in a string of characters.In the following example asterisk is used to match words that begins with m and ends with e −@”\bt\S*s\b”The following is the complete code −Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo {    public class Program {       private static void showMatch(string text, string expr) {          MatchCollection mc = Regex.Matches(text, expr);          foreach (Match m in mc) {             Console.WriteLine(m);          }     ... Read More

Exit Methods in C# Application

radhakrishna
Updated on 22-Jun-2020 13:16:20

10K+ Views

Environment.Exit() methodThe Environment.Exit() method terminates the process and returns an exit code to the operating system −Environment.Exit(exitCode);Use exitCode as 0 (zero) to show that the process completed successfully.Use exitCode as a non-zero number to show an error, for example −Environment.Exit(1) − Return a value 1 to show that the file you want is not presentEnvironment.Exit(2) − Return a value 2 to indicate that the file is in an incorrect format.System.Windows.Forms.Application.ExitThread( )To close a subapplication or current thread in a Windows Form, use theSystem.Windows.Forms.Application.ExitThread( ).private void buttonClose_Click(object sender, EventArgs eventArgs) {    System.Windows.Forms.Application.ExitThread( ); }Read More

How do you convert a list collection into an array in C#?

vanithasree
Updated on 22-Jun-2020 13:05:09

815 Views

Firstly, set a list collection −List < string > myList = new List < string > (); myList.Add("RedHat"); myList.Add("Ubuntu");Now, use ToArray() to convert the list to an array −string[] str = myList.ToArray();The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       List < string > myList = new List < string > ();       myList.Add("RedHat");       myList.Add("Ubuntu");       Console.WriteLine("List...");       foreach(string value in myList) {          Console.WriteLine(value);       } ... Read More

How to set a value to the element at the specified position in the one-dimensional array in C#

radhakrishna
Updated on 22-Jun-2020 13:05:58

299 Views

Firstly, set the array −int[] p = new int[] {55, 66, 88, 99, 111, 122, 133};Now, let us say you need to set an element at position 1 −p[2] = 77;Let us see the comple code −Example Live Demousing System; namespace Program {    public class Demo {       public static void Main(string[] args) {          int[] p = new int[] {55, 66, 88, 99, 111, 122, 133};          int j;          Console.WriteLine("Initial Array......");          for (j = 0; j < p.Length; j++ ) { ... Read More

Advertisements