language-features

What is an example of “this” assignment in C#?

让人想犯罪 __ 提交于 2019-11-27 06:40:24
问题 Does anybody have useful example of this assignment inside a C# method? I have been asked for it once during job interview, and I am still interested in answer myself. 回答1: The other answers are incorrect when they say you cannot assign to 'this'. True, you can't for a class type, but you can for a struct type: public struct MyValueType { public int Id; public void Swap(ref MyValueType other) { MyValueType temp = this; this = other; other = temp; } } At any point a struct can alter itself by

What are C macros useful for?

人盡茶涼 提交于 2019-11-27 06:04:43
I have written a little bit of C , and I can read it well enough to get a general idea of what it is doing, but every time I have encountered a macro it has thrown me completely. I end up having to remember what the macro is and substitute it in my head as I read. The ones that I have encountered that were intuitive and easy to understand were always like little mini functions, so I always wondered why they weren't just functions. I can understand the need to define different build types for debug or cross platform builds in the preprocessor but the ability to define arbitrary substitutions

Equivalent of Class Loaders in .NET

房东的猫 提交于 2019-11-27 06:02:18
Does anyone know if it possible to define the equivalent of a "java custom class loader" in .NET? To give a little background: I am in the process of developing a new programming language that targets the CLR, called "Liberty". One of the features of the language is its ability to define "type constructors", which are methods that are executed by the compiler at compile time and generate types as output. They are sort of a generalization of generics (the language does have normal generics in it), and allow code like this to be written (in "Liberty" syntax): var t as tuple<i as int, j as int, k

C# Null propagating operator / Conditional access expression & if blocks

梦想与她 提交于 2019-11-27 05:34:07
The Null propagating operator / Conditional access expression coming in c#-6.0 looks like quite a handy feature. But I'm curious if it will help solve the problem of checking if a child member is not null and then calling a Boolean method on said child member inside an if block: public class Container<int>{ IEnumerable<int> Objects {get;set;} } public Container BuildContainer() { var c = new Container(); if (/* Some Random Condition */) c.Objects = new List<int>{1,2,4}; } public void Test() { var c = BuildContainer(); //Old way if ( null != c && null != c.Objects && c.Objects.Any()) Console

VB.NET equivalent of C# property shorthand?

夙愿已清 提交于 2019-11-27 03:36:40
Is there a VB.NET equivalent to the C#: public string FirstName { get; set; } I know you can do Public Property name() As String Get Return _name.ToString End Get Set(ByVal value As String) _name = value End Set End Property But I can't seem to google up an answer on a Visual Basic shorthand. Stefan There is no shorthand for Visual Studio 2008 or prior for VB.NET. In Visual Studio 2010 and beyond, you can use the following shorthand: public property FirstName as String This will be handled as your short version in C# is - I think they call it "Auto Property" See also: Auto-Implemented

ForEach loop in Mathematica

こ雲淡風輕ζ 提交于 2019-11-27 03:12:27
问题 I'd like something like this: each[i_, {1,2,3}, Print[i] ] Or, more generally, to destructure arbitrary stuff in the list you're looping over, like: each[{i_, j_}, {{1,10}, {2,20}, {3,30}}, Print[i*j] ] Usually you want to use Map or other purely functional constructs and eschew a non-functional programming style where you use side effects. But here's an example where I think a for-each construct is supremely useful: Say I have a list of options (rules) that pair symbols with expressions,

Is it costly to do array.length or list.count in a loop

北战南征 提交于 2019-11-27 03:08:22
问题 I know that in JavaScript, creating a for loop like this: for(int i = 0; i < arr.length; i++) is costly as it computes the array length each time. Is this behavior costly in c# for lists and arrays as well. Or at compile-time is it optimized? Also what about other languages such as Java, how is this handled? 回答1: It is not costly in C#. For one thing, there is no “calculation“: querying the length is basically an elementary operation thanks to inlining. And secondly, because (according to its

When do I need to use Begin / End Blocks and the Go keyword in SQL Server?

我的未来我决定 提交于 2019-11-27 02:55:21
Can someone tell me when and where I need to use begin and end blocks in SQL Server? Also, what exactly does the Go keyword do? MatBailie GO is like the end of a script. You could have multiple CREATE TABLE statements, separated by GO. It's a way of isolating one part of the script from another, but submitting it all in one block. BEGIN and END are just like { and } in C/++/#, Java, etc. They bound a logical block of code. I tend to use BEGIN and END at the start and end of a stored procedure, but it's not strictly necessary there. Where it IS necessary is for loops, and IF statements, etc,

What is the purpose of python's inner classes?

匆匆过客 提交于 2019-11-27 02:45:34
Python's inner/nested classes confuse me. Is there something that can't be accomplished without them? If so, what is that thing? Aziz Quoted from http://www.geekinterview.com/question_details/64739 : Advantages of inner class: Logical grouping of classes : If a class is useful to only one other class then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined. Increased encapsulation : Consider two top-level classes A and B where B needs access to members of A that would otherwise be declared private. By hiding

What is the name of a [foo, bar] = [“foo”, “bar”] feature?

别等时光非礼了梦想. 提交于 2019-11-27 02:21:43
问题 I need to know a correct name for this cool feature that some languages provide. FYI: In some languages it is possible to do a multiple assignments by assigning a structure of values to a structure of "variables". In the example in the question title it assigns "foo" to foo and "bar" to bar. 回答1: It's generally called destructuring bind in functional languages (which don't have assignments) and destructuring assignment in imperative languages. Some languages provide subsets of that feature