Csharp Articles

Page 140 of 196

Managed code vs Unmanaged code in C#

Prabhas
Prabhas
Updated on 22-Jun-2020 4K+ 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

File Searching using C#

Samual Sam
Samual Sam
Updated on 22-Jun-2020 619 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

What is the C# equivalent of C++ friend keyword?

Arjun Thakur
Arjun Thakur
Updated on 22-Jun-2020 3K+ Views

friend in C#A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.C++ equivalent of friend in C#The closest equivalent is to create a nested class that will access the outer class private members.Here, the inner class can ...

Read More

How to convert a JavaScript array to C# array?

Ankith Reddy
Ankith Reddy
Updated on 22-Jun-2020 2K+ Views

Let us say our JavaScript array is −    var myArr = new Array(5);    myArr[0] = "Welcome";    myArr[1] = "to";    myArr[2] = "the";    myArr[3] = "Web";    myArr[4] = "World"; Now, convert the array into a string using comma as a separator −document.getElementById('demo1').value = myArr.join(',');Now, take this to C# −string[] str = demo.Split(",".ToCharArray());The above converts the JavaScript array to C#.

Read More

C# Equivalent to Java Functional Interfaces

mkotla
mkotla
Updated on 22-Jun-2020 3K+ Views

Equivalent of Java’s Functional Interfaces in C# is Delegates.Let us see the implementation of functional interface in Java −Example@FunctionalInterface public interface MyInterface {    void invoke(); } public class Demo {    void method(){       MyInterface x = () -> MyFunc ();       x.invoke();    }    void MyFunc() {    } }The same implementation in C# delagates −Examplepublic delegate void MyInterface (); public class Demo {    internal virtual void method() {       MyInterface x = () => MyFunc ();       x();    }    internal virtual void MyFunc() {    } }

Read More

C# Equivalent to Java's Double Brace Initialization?

usharani
usharani
Updated on 22-Jun-2020 354 Views

Java’s Double Brace Initialization does the same work what a single brace can achieve in C#.Double Brace creates and initialize objects in a single Java expression.Let’s say the following is in Java −ExampleList list = new List() {{    add("One");    add("Two");    add("Three");    add("Four"); }}The same you can use for Collection Initializer in C# as −List list = new List() {"One","Two", “Three”, “Four”};

Read More

Inheritance vs Composition in C#

Samual Sam
Samual Sam
Updated on 22-Jun-2020 753 Views

InheritanceWith Inheritance, you can designate that the new class should inherit the members of an existing class. This existing class is called the baseclass, and the new class is referred to as the derived class. Inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.For example, A base class Shape has a derived classes like Circle, Square, Rectangle, etc.CompositionUnder Composition, if the parent object is deleted, then the child object also loses its status. Composition is a special type of Aggregation and gives a part-of relationship.For example, ...

Read More

Local Inner Class in C#

karthikeya Boyini
karthikeya Boyini
Updated on 22-Jun-2020 1K+ Views

A nested class is a class declared in another enclosing class and it has inner as well as outer class. It is a member of its enclosing class and the members of an enclosing class have no access to members of a nested classLet us see an example code snippet of nested classes in C#.Here, class Two is a local inner class −Exampleclass One {    public int num1;    public class Two {       public int num2;    } } class Demo {    static void Main() {       One x = new One();   ...

Read More

How to open hidden file using C#?

George John
George John
Updated on 22-Jun-2020 766 Views

To open a hidden file, first make it visible. You can do this by removing the hidden attribute set on it −FileInfo file= new FileInfo(Environment.CurrentDirectory + @"\myFile.txt"); file.Attributes &= ~FileAttributes.Hidden;Now treat it as a normal text file and open it. Read the content −using (StreamReader sr = new StreamReader("myFile.txt")) {    string line;    while ((line = sr.ReadLine()) != null) {       Console.WriteLine(line);    } }After reading, set the attribute as hidden again to hide the file −file.Attributes |= FileAttributes.Hidden;

Read More

File Objects in C#

George John
George John
Updated on 22-Jun-2020 1K+ Views

To create a new file in C#, use the FileStream object.The following is the syntax −FileStream = new FileStream( , , , );Let us see an example for a file “test.dat”, which is created/ opened using File object −FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate,FileAccess.ReadWrite);The following is an example −Exampleusing System; using System.IO; namespace FileIOApplication {    class Program {       static void Main(string[] args) {          FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate,          FileAccess.ReadWrite);          for (int i = 1; i

Read More
Showing 1391–1400 of 1,951 articles
« Prev 1 138 139 140 141 142 196 Next »
Advertisements